Line data Source code
1 : /*
2 : * Copyright(c) 2019 Intel Corporation
3 : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 : */
5 :
6 : /*
7 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
8 : *
9 : * This source code is subject to the terms of the BSD 2 Clause License and
10 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
11 : * was not distributed with this source code in the LICENSE file, you can
12 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
13 : * Media Patent License 1.0 was not distributed with this source code in the
14 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
15 : */
16 : #include <stdlib.h>
17 :
18 : #include "EbDefinitions.h"
19 : #include "EbRateControlProcess.h"
20 : #include "EbSystemResourceManager.h"
21 : #include "EbSequenceControlSet.h"
22 : #include "EbPictureControlSet.h"
23 : #include "EbUtility.h"
24 : #include "EbSvtAv1ErrorCodes.h"
25 : #include "EbEntropyCoding.h"
26 :
27 : #include "EbRateControlResults.h"
28 : #include "EbRateControlTasks.h"
29 : #include "RateControlModel.h"
30 :
31 : #include "EbSegmentation.h"
32 :
33 : // calculate the QP based on the QP scaling
34 : uint32_t qp_scaling_calc(
35 : SequenceControlSet *sequence_control_set_ptr,
36 : EB_SLICE slice_type,
37 : uint32_t temporal_layer_index,
38 : uint32_t base_qp);
39 :
40 : /*****************************
41 : * Internal Typedefs
42 : *****************************/
43 0 : void rate_control_layer_reset(
44 : RateControlLayerContext *rate_control_layer_ptr,
45 : PictureControlSet *picture_control_set_ptr,
46 : RateControlContext *rate_control_context_ptr,
47 : uint32_t picture_area_in_pixel,
48 : EbBool was_used)
49 : {
50 0 : SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
51 : uint32_t slice_num;
52 : uint32_t temporal_layer_index;
53 : uint64_t total_frame_in_interval;
54 0 : uint64_t sum_bits_per_sw = 0;
55 :
56 0 : rate_control_layer_ptr->target_bit_rate = picture_control_set_ptr->parent_pcs_ptr->target_bit_rate*(uint64_t)rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][rate_control_layer_ptr->temporal_index] / 100;
57 : // update this based on temporal layers
58 0 : rate_control_layer_ptr->frame_rate = sequence_control_set_ptr->frame_rate;
59 :
60 0 : total_frame_in_interval = sequence_control_set_ptr->static_config.intra_period_length + 1;
61 :
62 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0 && sequence_control_set_ptr->intra_period_length != -1) {
63 0 : if (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
64 0 : total_frame_in_interval = 0;
65 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++) {
66 0 : rate_control_context_ptr->frames_in_interval[temporal_layer_index] = picture_control_set_ptr->parent_pcs_ptr->frames_in_interval[temporal_layer_index];
67 0 : total_frame_in_interval += picture_control_set_ptr->parent_pcs_ptr->frames_in_interval[temporal_layer_index];
68 0 : sum_bits_per_sw += picture_control_set_ptr->parent_pcs_ptr->bits_per_sw_per_layer[temporal_layer_index];
69 : }
70 : #if ADAPTIVE_PERCENTAGE
71 0 : rate_control_layer_ptr->target_bit_rate = picture_control_set_ptr->parent_pcs_ptr->target_bit_rate* picture_control_set_ptr->parent_pcs_ptr->bits_per_sw_per_layer[rate_control_layer_ptr->temporal_index] / sum_bits_per_sw;
72 : #endif
73 : }
74 : }
75 :
76 0 : if (sequence_control_set_ptr->static_config.intra_period_length != -1)
77 0 : rate_control_layer_ptr->frame_rate = sequence_control_set_ptr->frame_rate * rate_control_context_ptr->frames_in_interval[rate_control_layer_ptr->temporal_index] / total_frame_in_interval;
78 : else {
79 0 : switch (picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels) {
80 0 : case 0:
81 0 : break;
82 0 : case 1:
83 0 : if (sequence_control_set_ptr->static_config.intra_period_length == -1)
84 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> 1;
85 0 : break;
86 0 : case 2:
87 0 : if (rate_control_layer_ptr->temporal_index == 0)
88 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> 2;
89 : else
90 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> (3 - rate_control_layer_ptr->temporal_index);
91 0 : break;
92 0 : case 3:
93 0 : if (rate_control_layer_ptr->temporal_index == 0)
94 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> 3;
95 : else
96 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> (4 - rate_control_layer_ptr->temporal_index);
97 0 : break;
98 0 : case 4:
99 0 : if (rate_control_layer_ptr->temporal_index == 0)
100 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> 4;
101 : else
102 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> (5 - rate_control_layer_ptr->temporal_index);
103 0 : break;
104 0 : case 5:
105 0 : if (rate_control_layer_ptr->temporal_index == 0)
106 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> 5;
107 : else
108 0 : rate_control_layer_ptr->frame_rate = rate_control_layer_ptr->frame_rate >> (6 - rate_control_layer_ptr->temporal_index);
109 0 : break;
110 :
111 0 : default:
112 0 : break;
113 : }
114 : }
115 :
116 0 : rate_control_layer_ptr->coeff_averaging_weight1 = 5;
117 :
118 0 : rate_control_layer_ptr->coeff_averaging_weight2 = 16 - rate_control_layer_ptr->coeff_averaging_weight1;
119 0 : if (rate_control_layer_ptr->frame_rate == 0) { // no frame in that layer
120 0 : rate_control_layer_ptr->frame_rate = 1 << RC_PRECISION;
121 : }
122 0 : rate_control_layer_ptr->channel_bit_rate = (((rate_control_layer_ptr->target_bit_rate << (2 * RC_PRECISION)) / rate_control_layer_ptr->frame_rate) + RC_PRECISION_OFFSET) >> RC_PRECISION;
123 0 : rate_control_layer_ptr->channel_bit_rate = (uint64_t)MAX((int64_t)1, (int64_t)rate_control_layer_ptr->channel_bit_rate);
124 0 : rate_control_layer_ptr->ec_bit_constraint = rate_control_layer_ptr->channel_bit_rate;
125 :
126 : // This is only for the initial frame, because the feedback is from packetization now and all of these are considered
127 : // considering the bits for slice header
128 : // *Note - only one-slice-per picture is supported for UHD
129 0 : slice_num = 1;
130 :
131 0 : rate_control_layer_ptr->ec_bit_constraint -= SLICE_HEADER_BITS_NUM * slice_num;
132 :
133 0 : rate_control_layer_ptr->ec_bit_constraint = MAX(1, rate_control_layer_ptr->ec_bit_constraint);
134 :
135 0 : rate_control_layer_ptr->previous_bit_constraint = rate_control_layer_ptr->channel_bit_rate;
136 0 : rate_control_layer_ptr->bit_constraint = rate_control_layer_ptr->channel_bit_rate;
137 0 : rate_control_layer_ptr->dif_total_and_ec_bits = 0;
138 :
139 0 : rate_control_layer_ptr->frame_same_distortion_min_qp_count = 0;
140 0 : rate_control_layer_ptr->max_qp = picture_control_set_ptr->picture_qp;
141 :
142 0 : rate_control_layer_ptr->alpha = 1 << (RC_PRECISION - 1);
143 : {
144 0 : if (!was_used) {
145 0 : rate_control_layer_ptr->same_distortion_count = 0;
146 0 : rate_control_layer_ptr->k_coeff = 3 << RC_PRECISION;
147 0 : rate_control_layer_ptr->previous_k_coeff = 3 << RC_PRECISION;
148 0 : rate_control_layer_ptr->c_coeff = (rate_control_layer_ptr->channel_bit_rate << (2 * RC_PRECISION)) / picture_area_in_pixel / CCOEFF_INIT_FACT;
149 0 : rate_control_layer_ptr->previous_c_coeff = (rate_control_layer_ptr->channel_bit_rate << (2 * RC_PRECISION)) / picture_area_in_pixel / CCOEFF_INIT_FACT;
150 : // These are for handling Pred structure 2, when for higher temporal layer, frames can arrive in different orders
151 : // They should be modifed in a way that gets these from previous layers
152 0 : rate_control_layer_ptr->previous_frame_qp = 32;
153 0 : rate_control_layer_ptr->previous_frame_bit_actual = 1200;
154 0 : rate_control_layer_ptr->previous_framequantized_coeff_bit_actual = 1000;
155 0 : rate_control_layer_ptr->previous_frame_distortion_me = 10000000;
156 0 : rate_control_layer_ptr->previous_frame_qp = picture_control_set_ptr->picture_qp;
157 0 : rate_control_layer_ptr->delta_qp_fraction = 0;
158 0 : rate_control_layer_ptr->previous_frame_average_qp = picture_control_set_ptr->picture_qp;
159 0 : rate_control_layer_ptr->previous_calculated_frame_qp = picture_control_set_ptr->picture_qp;
160 0 : rate_control_layer_ptr->calculated_frame_qp = picture_control_set_ptr->picture_qp;
161 0 : rate_control_layer_ptr->critical_states = 0;
162 : }
163 : else {
164 0 : rate_control_layer_ptr->same_distortion_count = 0;
165 0 : rate_control_layer_ptr->critical_states = 0;
166 : }
167 : }
168 0 : }
169 :
170 0 : void rate_control_layer_reset_part2(
171 : RateControlContext *context_ptr,
172 : RateControlLayerContext *rate_control_layer_ptr,
173 : PictureControlSet *picture_control_set_ptr)
174 : {
175 : // update this based on temporal layers
176 0 : rate_control_layer_ptr->max_qp = (uint32_t)CLIP3(0, 63, (int32_t)context_ptr->qp_scaling_map[rate_control_layer_ptr->temporal_index][picture_control_set_ptr->picture_qp]);
177 : // These are for handling Pred structure 2, when for higher temporal layer, frames can arrive in different orders
178 : // They should be modifed in a way that gets these from previous layers
179 0 : rate_control_layer_ptr->previous_frame_qp = rate_control_layer_ptr->max_qp;
180 0 : rate_control_layer_ptr->previous_frame_average_qp = rate_control_layer_ptr->max_qp;
181 0 : rate_control_layer_ptr->previous_calculated_frame_qp = rate_control_layer_ptr->max_qp;
182 0 : rate_control_layer_ptr->calculated_frame_qp = rate_control_layer_ptr->max_qp;
183 0 : }
184 :
185 2 : EbErrorType high_level_rate_control_context_ctor(
186 : HighLevelRateControlContext *entry_ptr) {
187 : (void)entry_ptr;
188 :
189 2 : return EB_ErrorNone;
190 : }
191 :
192 3072 : EbErrorType rate_control_layer_context_ctor(
193 : RateControlLayerContext *entry_ptr) {
194 :
195 3072 : entry_ptr->first_frame = 1;
196 3072 : entry_ptr->first_non_intra_frame = 1;
197 :
198 3072 : return EB_ErrorNone;
199 : }
200 :
201 512 : void rate_control_interval_param_context_dctor(EbPtr p)
202 : {
203 512 : RateControlIntervalParamContext* obj = (RateControlIntervalParamContext*)p;
204 3584 : EB_DELETE_PTR_ARRAY(obj->rate_control_layer_array, EB_MAX_TEMPORAL_LAYERS);
205 512 : }
206 :
207 512 : EbErrorType rate_control_interval_param_context_ctor(
208 : RateControlIntervalParamContext *entry_ptr) {
209 : uint32_t temporal_index;
210 :
211 512 : entry_ptr->dctor = rate_control_interval_param_context_dctor;
212 :
213 512 : EB_ALLOC_PTR_ARRAY(entry_ptr->rate_control_layer_array, EB_MAX_TEMPORAL_LAYERS);
214 :
215 3584 : for (temporal_index = 0; temporal_index < EB_MAX_TEMPORAL_LAYERS; temporal_index++) {
216 3072 : EB_NEW(
217 : entry_ptr->rate_control_layer_array[temporal_index],
218 : rate_control_layer_context_ctor);
219 3072 : entry_ptr->rate_control_layer_array[temporal_index]->temporal_index = temporal_index;
220 3072 : entry_ptr->rate_control_layer_array[temporal_index]->frame_rate = 1 << RC_PRECISION;
221 : }
222 :
223 512 : return EB_ErrorNone;
224 : }
225 :
226 0 : EbErrorType rate_control_coded_frames_stats_context_ctor(
227 : CodedFramesStatsEntry *entry_ptr,
228 : uint64_t picture_number) {
229 :
230 0 : entry_ptr->picture_number = picture_number;
231 0 : entry_ptr->frame_total_bit_actual = -1;
232 :
233 0 : return EB_ErrorNone;
234 : }
235 :
236 2 : void rate_control_context_dctor(EbPtr p)
237 : {
238 2 : RateControlContext* obj = (RateControlContext*)p;
239 : #if OVERSHOOT_STAT_PRINT
240 : EB_DELETE_PTR_ARRAY(obj->coded_frames_stat_queue, CODED_FRAMES_STAT_QUEUE_MAX_DEPTH);
241 : #endif
242 514 : EB_DELETE_PTR_ARRAY(obj->rate_control_param_queue, PARALLEL_GOP_MAX_NUMBER);
243 2 : EB_DELETE(obj->high_level_rate_control_ptr);
244 2 : EB_DELETE(obj->rc_model_ptr);
245 :
246 2 : }
247 :
248 2 : EbErrorType rate_control_context_ctor(
249 : RateControlContext *context_ptr,
250 : EbFifo *rate_control_input_tasks_fifo_ptr,
251 : EbFifo *rate_control_output_results_fifo_ptr,
252 : int32_t intra_period)
253 : {
254 : uint32_t interval_index;
255 :
256 : #if OVERSHOOT_STAT_PRINT
257 : uint32_t picture_index;
258 : #endif
259 :
260 2 : context_ptr->dctor = rate_control_context_dctor;
261 2 : context_ptr->rate_control_input_tasks_fifo_ptr = rate_control_input_tasks_fifo_ptr;
262 2 : context_ptr->rate_control_output_results_fifo_ptr = rate_control_output_results_fifo_ptr;
263 :
264 : // High level RC
265 2 : EB_NEW(
266 : context_ptr->high_level_rate_control_ptr,
267 : high_level_rate_control_context_ctor);
268 :
269 2 : EB_NEW(context_ptr->rc_model_ptr, rate_control_model_ctor);
270 :
271 2 : EB_ALLOC_PTR_ARRAY(context_ptr->rate_control_param_queue, PARALLEL_GOP_MAX_NUMBER);
272 :
273 514 : for (interval_index = 0; interval_index < PARALLEL_GOP_MAX_NUMBER; interval_index++) {
274 512 : EB_NEW(
275 : context_ptr->rate_control_param_queue[interval_index],
276 : rate_control_interval_param_context_ctor);
277 512 : context_ptr->rate_control_param_queue[interval_index]->first_poc = (interval_index*(uint32_t)(intra_period + 1));
278 512 : context_ptr->rate_control_param_queue[interval_index]->last_poc = ((interval_index + 1)*(uint32_t)(intra_period + 1)) - 1;
279 : }
280 :
281 : #if OVERSHOOT_STAT_PRINT
282 : EB_ALLOC_PTR_ARRAY(context_ptr->coded_frames_stat_queue, CODED_FRAMES_STAT_QUEUE_MAX_DEPTH);
283 :
284 : for (picture_index = 0; picture_index < CODED_FRAMES_STAT_QUEUE_MAX_DEPTH; ++picture_index) {
285 : EB_NEW(context_ptr->coded_frames_stat_queue[picture_index],
286 : rate_control_coded_frames_stats_context_ctor,
287 : picture_index);
288 : }
289 : context_ptr->min_bit_actual_per_gop = 0xfffffffffffff;
290 : #endif
291 2 : context_ptr->intra_coef_rate = 4;
292 :
293 2 : return EB_ErrorNone;
294 : }
295 0 : uint64_t predict_bits(
296 : EncodeContext *encode_context_ptr,
297 : HlRateControlHistogramEntry *hl_rate_control_histogram_ptr_temp,
298 : uint32_t qp,
299 : uint32_t area_in_pixel)
300 : {
301 0 : uint64_t total_bits = 0;
302 :
303 0 : if (hl_rate_control_histogram_ptr_temp->is_coded) {
304 : // If the frame is already coded, use the actual number of bits
305 0 : total_bits = hl_rate_control_histogram_ptr_temp->total_num_bits_coded;
306 : }
307 : else {
308 0 : RateControlTables *rate_control_tables_ptr = &encode_context_ptr->rate_control_tables_array[qp];
309 0 : EbBitNumber *sad_bits_array_ptr = rate_control_tables_ptr->sad_bits_array[hl_rate_control_histogram_ptr_temp->temporal_layer_index];
310 0 : EbBitNumber *intra_sad_bits_array_ptr = rate_control_tables_ptr->intra_sad_bits_array[0];
311 0 : uint32_t pred_bits_ref_qp = 0;
312 :
313 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE) {
314 : // Loop over block in the frame and calculated the predicted bits at reg QP
315 : unsigned i;
316 0 : uint32_t accum = 0;
317 0 : for (i = 0; i < NUMBER_OF_INTRA_SAD_INTERVALS; ++i)
318 0 : accum += (uint32_t)((uint32_t)hl_rate_control_histogram_ptr_temp->ois_distortion_histogram[i] * (uint32_t)intra_sad_bits_array_ptr[i]);
319 0 : pred_bits_ref_qp = accum;
320 0 : total_bits += pred_bits_ref_qp;
321 : }
322 : else {
323 : unsigned i;
324 0 : uint32_t accum = 0;
325 0 : uint32_t accum_intra = 0;
326 0 : for (i = 0; i < NUMBER_OF_SAD_INTERVALS; ++i)
327 : {
328 0 : accum += (uint32_t)((uint32_t)hl_rate_control_histogram_ptr_temp->me_distortion_histogram[i] * (uint32_t)sad_bits_array_ptr[i]);
329 0 : accum_intra += (uint32_t)((uint32_t)hl_rate_control_histogram_ptr_temp->ois_distortion_histogram[i] * (uint32_t)intra_sad_bits_array_ptr[i]);
330 : }
331 0 : if (accum > accum_intra * 3)
332 0 : pred_bits_ref_qp = accum_intra;
333 : else
334 0 : pred_bits_ref_qp = accum;
335 0 : total_bits += pred_bits_ref_qp;
336 : }
337 :
338 : // Scale for in complete LCSs
339 : // total_bits is normalized based on the area because of the sbs at the picture boundries
340 0 : total_bits = total_bits * (uint64_t)area_in_pixel / (hl_rate_control_histogram_ptr_temp->full_sb_count << 12);
341 : }
342 0 : return total_bits;
343 : }
344 :
345 0 : void high_level_rc_input_picture_vbr(
346 : PictureParentControlSet *picture_control_set_ptr,
347 : SequenceControlSet *sequence_control_set_ptr,
348 : EncodeContext *encode_context_ptr,
349 : RateControlContext *context_ptr,
350 : HighLevelRateControlContext *high_level_rate_control_ptr)
351 : {
352 0 : EbBool end_of_sequence_flag = EB_TRUE;
353 :
354 : HlRateControlHistogramEntry *hl_rate_control_histogram_ptr_temp;
355 : // Queue variables
356 : uint32_t queue_entry_index_temp;
357 : uint32_t queue_entry_index_temp2;
358 : int64_t queue_entry_index_head_temp;
359 :
360 : uint64_t min_la_bit_distance;
361 : uint32_t selected_ref_qp_table_index;
362 : uint32_t selected_ref_qp;
363 : #if RC_UPDATE_TARGET_RATE
364 : uint32_t selected_org_ref_qp;
365 : #endif
366 0 : uint32_t previous_selected_ref_qp = encode_context_ptr->previous_selected_ref_qp;
367 0 : uint64_t max_coded_poc = encode_context_ptr->max_coded_poc;
368 0 : uint32_t max_coded_poc_selected_ref_qp = encode_context_ptr->max_coded_poc_selected_ref_qp;
369 :
370 : uint32_t ref_qp_index;
371 : uint32_t ref_qp_index_temp;
372 : uint32_t ref_qp_table_index;
373 :
374 : uint32_t area_in_pixel;
375 : uint32_t num_of_full_sbs;
376 : uint32_t qp_search_min;
377 : uint32_t qp_search_max;
378 0 : int32_t qp_step = 1;
379 : EbBool best_qp_found;
380 : uint32_t temporal_layer_index;
381 : EbBool tables_updated;
382 :
383 0 : uint64_t bit_constraint_per_sw = 0;
384 :
385 : RateControlTables *rate_control_tables_ptr;
386 : EbBitNumber *sad_bits_array_ptr;
387 : EbBitNumber *intra_sad_bits_array_ptr;
388 : uint32_t pred_bits_ref_qp;
389 :
390 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
391 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = 0;
392 0 : picture_control_set_ptr->total_bits_per_gop = 0;
393 :
394 0 : area_in_pixel = sequence_control_set_ptr->seq_header.max_frame_width * sequence_control_set_ptr->seq_header.max_frame_height;;
395 :
396 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->rate_table_update_mutex);
397 :
398 0 : tables_updated = sequence_control_set_ptr->encode_context_ptr->rate_control_tables_array_updated;
399 0 : picture_control_set_ptr->percentage_updated = EB_FALSE;
400 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
401 : // Increamenting the head of the hl_rate_control_historgram_queue and clean up the entores
402 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]);
403 0 : while ((hl_rate_control_histogram_ptr_temp->life_count == 0) && hl_rate_control_histogram_ptr_temp->passed_to_hlrc) {
404 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
405 : // Reset the Reorder Queue Entry
406 0 : hl_rate_control_histogram_ptr_temp->picture_number += INITIAL_RATE_CONTROL_REORDER_QUEUE_MAX_DEPTH;
407 0 : hl_rate_control_histogram_ptr_temp->life_count = -1;
408 0 : hl_rate_control_histogram_ptr_temp->passed_to_hlrc = EB_FALSE;
409 0 : hl_rate_control_histogram_ptr_temp->is_coded = EB_FALSE;
410 0 : hl_rate_control_histogram_ptr_temp->total_num_bits_coded = 0;
411 :
412 : // Increment the Reorder Queue head Ptr
413 0 : encode_context_ptr->hl_rate_control_historgram_queue_head_index =
414 0 : (encode_context_ptr->hl_rate_control_historgram_queue_head_index == HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? 0 : encode_context_ptr->hl_rate_control_historgram_queue_head_index + 1;
415 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
416 0 : hl_rate_control_histogram_ptr_temp = encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index];
417 : }
418 : // For the case that number of frames in the sliding window is less than size of the look ahead or intra Refresh. i.e. end of sequence
419 0 : if ((picture_control_set_ptr->frames_in_sw < MIN(sequence_control_set_ptr->static_config.look_ahead_distance + 1, (uint32_t)sequence_control_set_ptr->intra_period_length + 1))) {
420 0 : selected_ref_qp = max_coded_poc_selected_ref_qp;
421 :
422 : // Update the QP for the sliding window based on the status of RC
423 0 : if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 3)))
424 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 2, 0);
425 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 2)))
426 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 1, 0);
427 0 : if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 2)))
428 0 : selected_ref_qp += 2;
429 0 : else if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 1)))
430 0 : selected_ref_qp += 1;
431 0 : if ((picture_control_set_ptr->frames_in_sw < (uint32_t)(sequence_control_set_ptr->intra_period_length + 1)) &&
432 0 : (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0)) {
433 0 : selected_ref_qp++;
434 : }
435 :
436 0 : selected_ref_qp = (uint32_t)CLIP3(
437 : sequence_control_set_ptr->static_config.min_qp_allowed,
438 : sequence_control_set_ptr->static_config.max_qp_allowed,
439 : selected_ref_qp);
440 :
441 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
442 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
443 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
444 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
445 : (queue_entry_index_head_temp < 0) ?
446 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
447 : queue_entry_index_head_temp;
448 :
449 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
450 : {
451 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp]);
452 :
453 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
454 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
455 : else
456 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][selected_ref_qp];
457 :
458 0 : ref_qp_index_temp = (uint32_t)CLIP3(
459 : sequence_control_set_ptr->static_config.min_qp_allowed,
460 : sequence_control_set_ptr->static_config.max_qp_allowed,
461 : ref_qp_index_temp);
462 :
463 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = 0;
464 0 : rate_control_tables_ptr = &encode_context_ptr->rate_control_tables_array[ref_qp_index_temp];
465 0 : sad_bits_array_ptr = rate_control_tables_ptr->sad_bits_array[hl_rate_control_histogram_ptr_temp->temporal_layer_index];
466 0 : intra_sad_bits_array_ptr = rate_control_tables_ptr->intra_sad_bits_array[hl_rate_control_histogram_ptr_temp->temporal_layer_index];
467 0 : pred_bits_ref_qp = 0;
468 0 : num_of_full_sbs = 0;
469 :
470 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE) {
471 : // Loop over block in the frame and calculated the predicted bits at reg QP
472 : {
473 : unsigned i;
474 0 : uint32_t accum = 0;
475 0 : for (i = 0; i < NUMBER_OF_INTRA_SAD_INTERVALS; ++i)
476 0 : accum += (uint32_t)(hl_rate_control_histogram_ptr_temp->ois_distortion_histogram[i] * intra_sad_bits_array_ptr[i]);
477 0 : pred_bits_ref_qp = accum;
478 0 : num_of_full_sbs = hl_rate_control_histogram_ptr_temp->full_sb_count;
479 : }
480 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] += pred_bits_ref_qp;
481 : }
482 :
483 : else {
484 : {
485 : unsigned i;
486 0 : uint32_t accum = 0;
487 0 : for (i = 0; i < NUMBER_OF_SAD_INTERVALS; ++i)
488 0 : accum += (uint32_t)(hl_rate_control_histogram_ptr_temp->me_distortion_histogram[i] * sad_bits_array_ptr[i]);
489 0 : pred_bits_ref_qp = accum;
490 0 : num_of_full_sbs = hl_rate_control_histogram_ptr_temp->full_sb_count;
491 : }
492 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] += pred_bits_ref_qp;
493 : }
494 :
495 : // Scale for in complete
496 : // pred_bits_ref_qp is normalized based on the area because of the LCUs at the picture boundries
497 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] * (uint64_t)area_in_pixel / (num_of_full_sbs << 12);
498 :
499 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
500 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
501 : }
502 : }
503 : else {
504 : // Loop over the QPs and find the best QP
505 0 : min_la_bit_distance = MAX_UNSIGNED_VALUE;
506 0 : qp_search_min = (uint8_t)CLIP3(
507 : sequence_control_set_ptr->static_config.min_qp_allowed,
508 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
509 : (uint32_t)MAX((int32_t)sequence_control_set_ptr->qp - 40, 0));
510 :
511 0 : qp_search_max = (uint8_t)CLIP3(
512 : sequence_control_set_ptr->static_config.min_qp_allowed,
513 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
514 : sequence_control_set_ptr->qp + 40);
515 :
516 0 : for (ref_qp_table_index = qp_search_min; ref_qp_table_index < qp_search_max; ref_qp_table_index++)
517 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_table_index] = 0;
518 0 : bit_constraint_per_sw = high_level_rate_control_ptr->bit_constraint_per_sw * picture_control_set_ptr->frames_in_sw / (sequence_control_set_ptr->static_config.look_ahead_distance + 1);
519 :
520 : // Update the target rate for the sliding window based on the status of RC
521 0 : if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size * 10)))
522 0 : bit_constraint_per_sw = bit_constraint_per_sw * 130 / 100;
523 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 3)))
524 0 : bit_constraint_per_sw = bit_constraint_per_sw * 120 / 100;
525 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 2)))
526 0 : bit_constraint_per_sw = bit_constraint_per_sw * 110 / 100;
527 0 : if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 3)))
528 0 : bit_constraint_per_sw = bit_constraint_per_sw * 80 / 100;
529 0 : else if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 2)))
530 0 : bit_constraint_per_sw = bit_constraint_per_sw * 90 / 100;
531 : // Loop over proper QPs and find the Predicted bits for that QP. Find the QP with the closest total predicted rate to target bits for the sliding window.
532 0 : previous_selected_ref_qp = CLIP3(
533 : qp_search_min,
534 : qp_search_max,
535 : previous_selected_ref_qp);
536 0 : ref_qp_table_index = previous_selected_ref_qp;
537 0 : selected_ref_qp_table_index = ref_qp_table_index;
538 0 : selected_ref_qp = selected_ref_qp_table_index;
539 0 : best_qp_found = EB_FALSE;
540 0 : while (ref_qp_table_index >= qp_search_min && ref_qp_table_index <= qp_search_max && !best_qp_found) {
541 0 : ref_qp_index = CLIP3(
542 : sequence_control_set_ptr->static_config.min_qp_allowed,
543 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
544 : ref_qp_table_index);
545 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] = 0;
546 :
547 : // Finding the predicted bits for each frame in the sliding window at the reference Qp(s)
548 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
549 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
550 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
551 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
552 : (queue_entry_index_head_temp < 0) ?
553 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
554 : queue_entry_index_head_temp;
555 :
556 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
557 : // This is set to false, so the last frame would go inside the loop
558 0 : end_of_sequence_flag = EB_FALSE;
559 :
560 0 : while (!end_of_sequence_flag &&
561 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->static_config.look_ahead_distance) {
562 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
563 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
564 :
565 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
566 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[ref_qp_index];
567 : else
568 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][ref_qp_index];
569 :
570 0 : ref_qp_index_temp = (uint32_t)CLIP3(
571 : sequence_control_set_ptr->static_config.min_qp_allowed,
572 : sequence_control_set_ptr->static_config.max_qp_allowed,
573 : ref_qp_index_temp);
574 :
575 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = 0;
576 :
577 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
578 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
579 0 : hl_rate_control_histogram_ptr_temp->life_count--;
580 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
581 : }
582 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = predict_bits(
583 : encode_context_ptr,
584 : hl_rate_control_histogram_ptr_temp,
585 : ref_qp_index_temp,
586 : area_in_pixel);
587 :
588 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
589 :
590 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
591 0 : if (queue_entry_index_head_temp == queue_entry_index_temp2)
592 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
593 :
594 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
595 0 : queue_entry_index_temp++;
596 : }
597 :
598 0 : if (min_la_bit_distance >= (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw)) {
599 0 : min_la_bit_distance = (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw);
600 0 : selected_ref_qp_table_index = ref_qp_table_index;
601 0 : selected_ref_qp = ref_qp_index;
602 : }
603 : else
604 0 : best_qp_found = EB_TRUE;
605 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
606 0 : if (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] > bit_constraint_per_sw)
607 0 : qp_step = +1;
608 : else
609 0 : qp_step = -1;
610 : }
611 0 : ref_qp_table_index = (uint32_t)(ref_qp_table_index + qp_step);
612 : }
613 : }
614 :
615 : #if RC_UPDATE_TARGET_RATE
616 0 : selected_org_ref_qp = selected_ref_qp;
617 0 : if (sequence_control_set_ptr->intra_period_length != -1 && picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0 &&
618 0 : (int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length) {
619 0 : if (picture_control_set_ptr->picture_number > 0)
620 0 : picture_control_set_ptr->intra_selected_org_qp = (uint8_t)selected_ref_qp;
621 0 : ref_qp_index = selected_ref_qp;
622 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] = 0;
623 :
624 0 : if (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] == 0) {
625 : // Finding the predicted bits for each frame in the sliding window at the reference Qp(s)
626 : //queue_entry_index_temp = encode_context_ptr->hl_rate_control_historgram_queue_head_index;
627 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
628 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
629 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
630 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
631 : (queue_entry_index_head_temp < 0) ?
632 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
633 : queue_entry_index_head_temp;
634 :
635 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
636 :
637 : // This is set to false, so the last frame would go inside the loop
638 0 : end_of_sequence_flag = EB_FALSE;
639 :
640 0 : while (!end_of_sequence_flag &&
641 : //queue_entry_index_temp <= encode_context_ptr->hl_rate_control_historgram_queue_head_index+sequence_control_set_ptr->static_config.look_ahead_distance){
642 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->static_config.look_ahead_distance) {
643 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
644 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
645 :
646 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
647 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[ref_qp_index];
648 : else
649 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][ref_qp_index];
650 :
651 0 : ref_qp_index_temp = (uint32_t)CLIP3(
652 : sequence_control_set_ptr->static_config.min_qp_allowed,
653 : sequence_control_set_ptr->static_config.max_qp_allowed,
654 : ref_qp_index_temp);
655 :
656 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = predict_bits(
657 : encode_context_ptr,
658 : hl_rate_control_histogram_ptr_temp,
659 : ref_qp_index_temp,
660 : area_in_pixel);
661 :
662 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
663 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
664 : // if(encode_context_ptr->hl_rate_control_historgram_queue_head_index == queue_entry_index_temp2)
665 0 : if (queue_entry_index_head_temp == queue_entry_index_temp2)
666 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
667 :
668 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
669 0 : queue_entry_index_temp++;
670 : }
671 : }
672 : }
673 : #endif
674 0 : picture_control_set_ptr->tables_updated = tables_updated;
675 0 : EbBool expensive_i_slice = EB_FALSE;
676 : // Looping over the window to find the percentage of bit allocation in each layer
677 0 : if ((sequence_control_set_ptr->intra_period_length != -1) &&
678 0 : ((int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length) &&
679 0 : ((int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length)) {
680 0 : uint64_t i_slice_bits = 0;
681 :
682 0 : if (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
683 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
684 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
685 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
686 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
687 : (queue_entry_index_head_temp < 0) ?
688 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
689 : queue_entry_index_head_temp;
690 :
691 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
692 :
693 : // This is set to false, so the last frame would go inside the loop
694 0 : end_of_sequence_flag = EB_FALSE;
695 :
696 0 : while (!end_of_sequence_flag &&
697 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->intra_period_length) {
698 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
699 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
700 :
701 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
702 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
703 : else
704 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][selected_ref_qp];
705 :
706 0 : ref_qp_index_temp = (uint32_t)CLIP3(
707 : sequence_control_set_ptr->static_config.min_qp_allowed,
708 : sequence_control_set_ptr->static_config.max_qp_allowed,
709 : ref_qp_index_temp);
710 :
711 0 : if (queue_entry_index_temp == queue_entry_index_head_temp)
712 0 : i_slice_bits = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
713 0 : picture_control_set_ptr->total_bits_per_gop += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
714 0 : picture_control_set_ptr->bits_per_sw_per_layer[hl_rate_control_histogram_ptr_temp->temporal_layer_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
715 0 : picture_control_set_ptr->percentage_updated = EB_TRUE;
716 :
717 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
718 0 : queue_entry_index_temp++;
719 : }
720 0 : if (i_slice_bits * 100 > 85 * picture_control_set_ptr->total_bits_per_gop)
721 0 : expensive_i_slice = EB_TRUE;
722 0 : if (picture_control_set_ptr->total_bits_per_gop == 0) {
723 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
724 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_index];
725 : }
726 : }
727 : }
728 : else {
729 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
730 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_index];
731 : }
732 0 : if (expensive_i_slice) {
733 0 : if (tables_updated)
734 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 1, 0);
735 : else
736 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 3, 0);
737 0 : selected_ref_qp = (uint32_t)CLIP3(
738 : sequence_control_set_ptr->static_config.min_qp_allowed,
739 : sequence_control_set_ptr->static_config.max_qp_allowed,
740 : selected_ref_qp);
741 : }
742 : // Set the QP
743 0 : previous_selected_ref_qp = selected_ref_qp;
744 0 : if (picture_control_set_ptr->picture_number > max_coded_poc && picture_control_set_ptr->temporal_layer_index < 2 && !picture_control_set_ptr->end_of_sequence_region) {
745 0 : max_coded_poc = picture_control_set_ptr->picture_number;
746 0 : max_coded_poc_selected_ref_qp = previous_selected_ref_qp;
747 0 : encode_context_ptr->previous_selected_ref_qp = previous_selected_ref_qp;
748 0 : encode_context_ptr->max_coded_poc = max_coded_poc;
749 0 : encode_context_ptr->max_coded_poc_selected_ref_qp = max_coded_poc_selected_ref_qp;
750 : }
751 :
752 0 : if (picture_control_set_ptr->slice_type == I_SLICE)
753 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
754 : else
755 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][selected_ref_qp];
756 :
757 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)CLIP3(
758 : sequence_control_set_ptr->static_config.min_qp_allowed,
759 : sequence_control_set_ptr->static_config.max_qp_allowed,
760 : picture_control_set_ptr->best_pred_qp);
761 :
762 : #if RC_UPDATE_TARGET_RATE
763 0 : if (picture_control_set_ptr->picture_number == 0) {
764 0 : high_level_rate_control_ptr->prev_intra_selected_ref_qp = selected_ref_qp;
765 0 : high_level_rate_control_ptr->prev_intra_org_selected_ref_qp = selected_ref_qp;
766 : }
767 0 : if (sequence_control_set_ptr->intra_period_length != -1) {
768 0 : if (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
769 0 : high_level_rate_control_ptr->prev_intra_selected_ref_qp = selected_ref_qp;
770 0 : high_level_rate_control_ptr->prev_intra_org_selected_ref_qp = selected_org_ref_qp;
771 : }
772 : }
773 : #endif
774 0 : picture_control_set_ptr->target_bits_best_pred_qp = picture_control_set_ptr->pred_bits_ref_qp[picture_control_set_ptr->best_pred_qp];
775 : #if RC_PRINTS
776 : if (picture_control_set_ptr->slice_type == 2)
777 : {
778 : SVT_LOG("\nTID: %d\t", picture_control_set_ptr->temporal_layer_index);
779 : SVT_LOG("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",
780 : picture_control_set_ptr->picture_number,
781 : picture_control_set_ptr->best_pred_qp,
782 : selected_ref_qp,
783 : (int)picture_control_set_ptr->target_bits_best_pred_qp,
784 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp - 1],
785 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp],
786 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp + 1],
787 : (int)high_level_rate_control_ptr->bit_constraint_per_sw,
788 : (int)bit_constraint_per_sw/*,
789 : (int)high_level_rate_control_ptr->virtual_buffer_level*/);
790 : }
791 : #endif
792 : }
793 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->rate_table_update_mutex);
794 0 : }
795 0 : void frame_level_rc_input_picture_vbr(
796 : PictureControlSet *picture_control_set_ptr,
797 : SequenceControlSet *sequence_control_set_ptr,
798 : RateControlContext *context_ptr,
799 : RateControlLayerContext *rate_control_layer_ptr,
800 : RateControlIntervalParamContext *rate_control_param_ptr)
801 : {
802 : RateControlLayerContext *rate_control_layer_temp_ptr;
803 :
804 : // Tiles
805 : uint32_t picture_area_in_pixel;
806 : uint32_t area_in_pixel;
807 :
808 : // SB Loop variables
809 : SbParams *sb_params_ptr;
810 : uint32_t sb_index;
811 : uint64_t temp_qp;
812 : uint32_t area_in_sbs;
813 :
814 0 : picture_area_in_pixel = sequence_control_set_ptr->seq_header.max_frame_height*sequence_control_set_ptr->seq_header.max_frame_width;
815 :
816 0 : if (rate_control_layer_ptr->first_frame == 1) {
817 0 : rate_control_layer_ptr->first_frame = 0;
818 0 : picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer = 1;
819 : }
820 : else
821 0 : picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer = 0;
822 0 : if (picture_control_set_ptr->slice_type != I_SLICE) {
823 0 : if (rate_control_layer_ptr->first_non_intra_frame == 1) {
824 0 : rate_control_layer_ptr->first_non_intra_frame = 0;
825 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 1;
826 : }
827 : else
828 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 0;
829 : }
830 : else
831 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 0;
832 :
833 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = 0;
834 :
835 : // ***Rate Control***
836 0 : area_in_sbs = 0;
837 0 : area_in_pixel = 0;
838 :
839 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
840 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
841 :
842 0 : if (sb_params_ptr->is_complete_sb) {
843 : // add the area of one LCU (64x64=4096) to the area of the tile
844 0 : area_in_pixel += 4096;
845 0 : area_in_sbs++;
846 : }
847 : else {
848 : // add the area of the LCU to the area of the tile
849 0 : area_in_pixel += sb_params_ptr->width * sb_params_ptr->height;
850 : }
851 : }
852 0 : rate_control_layer_ptr->area_in_pixel = area_in_pixel;
853 :
854 0 : if (picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer || (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc)) {
855 0 : if (sequence_control_set_ptr->static_config.enable_qp_scaling_flag && (picture_control_set_ptr->picture_number != rate_control_param_ptr->first_poc)) {
856 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
857 : (int32_t)sequence_control_set_ptr->static_config.min_qp_allowed,
858 : (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed,
859 : (int32_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]));
860 : }
861 :
862 0 : if (picture_control_set_ptr->picture_number == 0) {
863 0 : rate_control_param_ptr->intra_frames_qp = sequence_control_set_ptr->qp;
864 0 : rate_control_param_ptr->intra_frames_qp_bef_scal = (uint8_t)sequence_control_set_ptr->qp;
865 : }
866 :
867 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
868 : uint32_t temporal_layer_idex;
869 0 : rate_control_param_ptr->previous_virtual_buffer_level = context_ptr->virtual_buffer_level_initial_value;
870 0 : rate_control_param_ptr->virtual_buffer_level = context_ptr->virtual_buffer_level_initial_value;
871 0 : rate_control_param_ptr->extra_ap_bit_ratio_i = 0;
872 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
873 0 : rate_control_param_ptr->last_poc = MAX(rate_control_param_ptr->first_poc + picture_control_set_ptr->parent_pcs_ptr->frames_in_sw - 1, rate_control_param_ptr->first_poc);
874 0 : rate_control_param_ptr->last_gop = EB_TRUE;
875 : }
876 :
877 0 : if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size >> 8)) ||
878 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size >> 8))) {
879 0 : int64_t extra_bits_per_gop = 0;
880 :
881 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
882 0 : if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 4)) ||
883 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 4))) {
884 0 : extra_bits_per_gop = context_ptr->extra_bits;
885 0 : extra_bits_per_gop = CLIP3(
886 : -(int64_t)(context_ptr->vb_fill_threshold2 << 3),
887 : (int64_t)(context_ptr->vb_fill_threshold2 << 3),
888 : extra_bits_per_gop);
889 : }
890 : else
891 0 : if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 3)) ||
892 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 3))) {
893 0 : extra_bits_per_gop = context_ptr->extra_bits;
894 0 : extra_bits_per_gop = CLIP3(
895 : -(int64_t)(context_ptr->vb_fill_threshold2 << 2),
896 : (int64_t)(context_ptr->vb_fill_threshold2 << 2),
897 : extra_bits_per_gop);
898 : }
899 0 : else if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 2)) ||
900 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 2))) {
901 0 : extra_bits_per_gop = CLIP3(
902 : -(int64_t)context_ptr->vb_fill_threshold2 << 1,
903 : (int64_t)context_ptr->vb_fill_threshold2 << 1,
904 : extra_bits_per_gop);
905 : }
906 : else {
907 0 : extra_bits_per_gop = CLIP3(
908 : -(int64_t)context_ptr->vb_fill_threshold1,
909 : (int64_t)context_ptr->vb_fill_threshold1,
910 : extra_bits_per_gop);
911 : }
912 : }
913 : else {
914 0 : if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 3)) ||
915 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 3))) {
916 0 : extra_bits_per_gop = context_ptr->extra_bits;
917 0 : extra_bits_per_gop = CLIP3(
918 : -(int64_t)(context_ptr->vb_fill_threshold2 << 2),
919 : (int64_t)(context_ptr->vb_fill_threshold2 << 2),
920 : extra_bits_per_gop);
921 : }
922 0 : else if ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 2)) ||
923 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 2))) {
924 0 : extra_bits_per_gop = CLIP3(
925 : -(int64_t)context_ptr->vb_fill_threshold2 << 1,
926 : (int64_t)context_ptr->vb_fill_threshold2 << 1,
927 : extra_bits_per_gop);
928 : }
929 : }
930 :
931 0 : rate_control_param_ptr->virtual_buffer_level -= extra_bits_per_gop;
932 0 : rate_control_param_ptr->previous_virtual_buffer_level -= extra_bits_per_gop;
933 0 : context_ptr->extra_bits -= extra_bits_per_gop;
934 : }
935 :
936 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
937 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
938 0 : rate_control_layer_reset(
939 : rate_control_layer_temp_ptr,
940 : picture_control_set_ptr,
941 : context_ptr,
942 : picture_area_in_pixel,
943 0 : rate_control_param_ptr->was_used);
944 : }
945 : }
946 :
947 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = 0;
948 : // Finding the QP of the Intra frame by using variance tables
949 0 : if (picture_control_set_ptr->slice_type == I_SLICE) {
950 : uint32_t selected_ref_qp;
951 :
952 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance == 0)
953 0 : printf("ERROR: LAD=0 is not supported\n");
954 : else {
955 0 : selected_ref_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
956 0 : picture_control_set_ptr->picture_qp = (uint8_t)selected_ref_qp;
957 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->picture_qp;
958 : }
959 :
960 : // Update the QP based on the VB
961 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
962 0 : if (rate_control_param_ptr->virtual_buffer_level >= context_ptr->vb_fill_threshold2 << 1)
963 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 2;
964 0 : else if (rate_control_param_ptr->virtual_buffer_level >= context_ptr->vb_fill_threshold2)
965 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE;
966 0 : else if (rate_control_param_ptr->virtual_buffer_level >= context_ptr->vb_fill_threshold1 &&
967 0 : rate_control_param_ptr->virtual_buffer_level < context_ptr->vb_fill_threshold2) {
968 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD1QPINCREASE;
969 : }
970 0 : if (rate_control_param_ptr->virtual_buffer_level <= -(context_ptr->vb_fill_threshold2 << 2))
971 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - (int32_t)2, 0);
972 : else
973 0 : if (rate_control_param_ptr->virtual_buffer_level <= -(context_ptr->vb_fill_threshold2 << 1))
974 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - (int32_t)1, 0);
975 0 : else if (rate_control_param_ptr->virtual_buffer_level <= 0)
976 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
977 : }
978 : else {
979 0 : if (rate_control_param_ptr->virtual_buffer_level >= context_ptr->vb_fill_threshold2)
980 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE;
981 0 : if (rate_control_param_ptr->virtual_buffer_level <= -(context_ptr->vb_fill_threshold2 << 2))
982 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp - (uint8_t)THRESHOLD2QPINCREASE - (int32_t)2;
983 0 : else if (rate_control_param_ptr->virtual_buffer_level <= -(context_ptr->vb_fill_threshold2 << 1))
984 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp - (uint8_t)THRESHOLD2QPINCREASE - (int32_t)1;
985 : else
986 0 : if (rate_control_param_ptr->virtual_buffer_level <= 0)
987 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
988 : }
989 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
990 : sequence_control_set_ptr->static_config.min_qp_allowed,
991 : sequence_control_set_ptr->static_config.max_qp_allowed,
992 : picture_control_set_ptr->picture_qp);
993 : }
994 : else {
995 : // LCU Loop
996 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
997 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
998 :
999 0 : if (sb_params_ptr->is_complete_sb)
1000 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me += picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_index];
1001 : }
1002 :
1003 : // tileSadMe is normalized based on the area because of the LCUs at the tile boundries
1004 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me*rate_control_layer_ptr->area_in_pixel / (area_in_sbs << 12)), 1);
1005 :
1006 : // totalSquareMad has RC_PRECISION precision
1007 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me <<= RC_PRECISION;
1008 : }
1009 :
1010 0 : temp_qp = picture_control_set_ptr->picture_qp;
1011 :
1012 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
1013 : uint32_t temporal_layer_idex;
1014 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
1015 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
1016 0 : rate_control_layer_reset_part2(
1017 : context_ptr,
1018 : rate_control_layer_temp_ptr,
1019 : picture_control_set_ptr);
1020 : }
1021 : }
1022 :
1023 0 : if (picture_control_set_ptr->picture_number == 0) {
1024 0 : context_ptr->base_layer_frames_avg_qp = picture_control_set_ptr->picture_qp + 1;
1025 0 : context_ptr->base_layer_intra_frames_avg_qp = picture_control_set_ptr->picture_qp;
1026 : }
1027 : }
1028 : else {
1029 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = 0;
1030 :
1031 : // if the pixture is an I slice, for now we set the QP as the QP of the previous frame
1032 0 : if (picture_control_set_ptr->slice_type == I_SLICE) {
1033 : uint32_t selected_ref_qp;
1034 :
1035 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance == 0)
1036 0 : printf("ERROR: LAD=0 is not supported\n");
1037 : else {
1038 0 : selected_ref_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
1039 0 : picture_control_set_ptr->picture_qp = (uint8_t)selected_ref_qp;
1040 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->picture_qp;
1041 : }
1042 :
1043 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1044 : sequence_control_set_ptr->static_config.min_qp_allowed,
1045 : sequence_control_set_ptr->static_config.max_qp_allowed,
1046 : picture_control_set_ptr->picture_qp);
1047 :
1048 0 : temp_qp = picture_control_set_ptr->picture_qp;
1049 : }
1050 :
1051 : else { // Not an I slice
1052 : // combining the target rate from initial RC and frame level RC
1053 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
1054 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = rate_control_layer_ptr->bit_constraint;
1055 0 : rate_control_layer_ptr->ec_bit_constraint = (rate_control_layer_ptr->alpha * picture_control_set_ptr->parent_pcs_ptr->target_bits_best_pred_qp +
1056 0 : ((1 << RC_PRECISION) - rate_control_layer_ptr->alpha) * picture_control_set_ptr->parent_pcs_ptr->target_bits_rc + RC_PRECISION_OFFSET) >> RC_PRECISION;
1057 :
1058 0 : rate_control_layer_ptr->ec_bit_constraint = (uint64_t)MAX((int64_t)rate_control_layer_ptr->ec_bit_constraint - (int64_t)rate_control_layer_ptr->dif_total_and_ec_bits, 1);
1059 :
1060 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = rate_control_layer_ptr->ec_bit_constraint;
1061 : }
1062 :
1063 : // LCU Loop
1064 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
1065 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
1066 :
1067 0 : if (sb_params_ptr->is_complete_sb)
1068 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me += picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_index];
1069 : }
1070 :
1071 : // tileSadMe is normalized based on the area because of the LCUs at the tile boundries
1072 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me*rate_control_layer_ptr->area_in_pixel / (area_in_sbs << 12)), 1);
1073 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me <<= RC_PRECISION;
1074 0 : if (rate_control_layer_ptr->area_in_pixel > 0)
1075 0 : rate_control_layer_ptr->total_mad = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me / rate_control_layer_ptr->area_in_pixel), 1);
1076 0 : if (!rate_control_layer_ptr->feedback_arrived)
1077 0 : rate_control_layer_ptr->previous_frame_distortion_me = picture_control_set_ptr->parent_pcs_ptr->sad_me;
1078 : {
1079 : uint64_t qp_calc_temp1, qp_calc_temp2, qp_calc_temp3;
1080 :
1081 0 : qp_calc_temp1 = picture_control_set_ptr->parent_pcs_ptr->sad_me *rate_control_layer_ptr->total_mad;
1082 0 : qp_calc_temp2 =
1083 0 : MAX((int64_t)(rate_control_layer_ptr->ec_bit_constraint << (2 * RC_PRECISION)) - (int64_t)rate_control_layer_ptr->c_coeff*(int64_t)rate_control_layer_ptr->area_in_pixel,
1084 : (int64_t)(rate_control_layer_ptr->ec_bit_constraint << (2 * RC_PRECISION - 2)));
1085 :
1086 : // This is a more complex but with higher precision implementation
1087 0 : if (qp_calc_temp1 > qp_calc_temp2)
1088 0 : qp_calc_temp3 = (uint64_t)((qp_calc_temp1 / qp_calc_temp2)*rate_control_layer_ptr->k_coeff);
1089 : else
1090 0 : qp_calc_temp3 = (uint64_t)(qp_calc_temp1*rate_control_layer_ptr->k_coeff / qp_calc_temp2);
1091 0 : temp_qp = (uint64_t)(log2f_high_precision(MAX(((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION)*((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION)*((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION), 1), RC_PRECISION));
1092 :
1093 0 : rate_control_layer_ptr->calculated_frame_qp = (uint8_t)(CLIP3(1, 63, (uint32_t)(temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION));
1094 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = (uint8_t)(CLIP3(1, 63, (uint32_t)(temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION));
1095 : }
1096 :
1097 0 : temp_qp += rate_control_layer_ptr->delta_qp_fraction;
1098 0 : picture_control_set_ptr->picture_qp = (uint8_t)((temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION);
1099 : // Use the QP of HLRC instead of calculated one in FLRC
1100 0 : if (picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels > 1) {
1101 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
1102 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
1103 : }
1104 : }
1105 0 : if (picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer && picture_control_set_ptr->temporal_layer_index == 0 && picture_control_set_ptr->slice_type != I_SLICE)
1106 0 : picture_control_set_ptr->picture_qp = (uint8_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]);
1107 0 : if (!rate_control_layer_ptr->feedback_arrived && picture_control_set_ptr->slice_type != I_SLICE) {
1108 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1109 : (int32_t)sequence_control_set_ptr->static_config.min_qp_allowed,
1110 : (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed,
1111 : (int32_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]));
1112 : }
1113 :
1114 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
1115 0 : if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 2)
1116 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 4;
1117 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 1)
1118 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 3;
1119 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2)
1120 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 2;
1121 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold1 &&
1122 0 : rate_control_param_ptr->virtual_buffer_level < context_ptr->vb_fill_threshold2) {
1123 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD1QPINCREASE + 2;
1124 : }
1125 : }
1126 : else {
1127 0 : if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 2)
1128 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 2;
1129 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 1)
1130 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 1;
1131 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2)
1132 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 1;
1133 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold1 &&
1134 0 : rate_control_param_ptr->virtual_buffer_level < context_ptr->vb_fill_threshold2) {
1135 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD1QPINCREASE;
1136 : }
1137 : }
1138 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
1139 0 : if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 2))
1140 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 2, 0);
1141 0 : else if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 1))
1142 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 1, 0);
1143 0 : else if (rate_control_param_ptr->virtual_buffer_level < 0)
1144 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
1145 : }
1146 : else {
1147 0 : if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 2))
1148 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 1, 0);
1149 0 : else if (rate_control_param_ptr->virtual_buffer_level < -context_ptr->vb_fill_threshold2)
1150 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
1151 : }
1152 :
1153 : // limiting the QP based on the predicted QP
1154 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
1155 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
1156 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1157 : (uint32_t)MAX((int32_t)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp - 8, 0),
1158 : (uint32_t)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp + 8,
1159 : (uint32_t)picture_control_set_ptr->picture_qp);
1160 : }
1161 : else {
1162 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1163 : (uint32_t)MAX((int32_t)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp - 8, 0),
1164 : (uint32_t)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp + 8,
1165 : (uint32_t)picture_control_set_ptr->picture_qp);
1166 : }
1167 : }
1168 0 : if (picture_control_set_ptr->picture_number != rate_control_param_ptr->first_poc &&
1169 0 : picture_control_set_ptr->picture_qp == picture_control_set_ptr->parent_pcs_ptr->best_pred_qp && rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold1) {
1170 0 : if (rate_control_param_ptr->extra_ap_bit_ratio_i > 200)
1171 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + 3;
1172 0 : else if (rate_control_param_ptr->extra_ap_bit_ratio_i > 100)
1173 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + 2;
1174 0 : else if (rate_control_param_ptr->extra_ap_bit_ratio_i > 50)
1175 0 : picture_control_set_ptr->picture_qp++;
1176 : }
1177 : //Limiting the QP based on the QP of the Reference frame
1178 :
1179 : uint32_t ref_qp;
1180 0 : if ((int32_t)picture_control_set_ptr->temporal_layer_index == 0 && picture_control_set_ptr->slice_type != I_SLICE) {
1181 0 : if (picture_control_set_ptr->ref_slice_type_array[0][0] == I_SLICE) {
1182 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1183 : (uint32_t)picture_control_set_ptr->ref_pic_qp_array[0][0],
1184 : (uint32_t)picture_control_set_ptr->picture_qp,
1185 : picture_control_set_ptr->picture_qp);
1186 : }
1187 : else {
1188 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1189 : (uint32_t)MAX((int32_t)picture_control_set_ptr->ref_pic_qp_array[0][0] - 1, 0),
1190 : (uint32_t)picture_control_set_ptr->picture_qp,
1191 : picture_control_set_ptr->picture_qp);
1192 : }
1193 : }
1194 : else {
1195 0 : ref_qp = 0;
1196 0 : if (picture_control_set_ptr->ref_slice_type_array[0][0] != I_SLICE)
1197 0 : ref_qp = MAX(ref_qp, picture_control_set_ptr->ref_pic_qp_array[0][0]);
1198 0 : if ((picture_control_set_ptr->slice_type == B_SLICE) && (picture_control_set_ptr->ref_slice_type_array[1][0] != I_SLICE))
1199 0 : ref_qp = MAX(ref_qp, picture_control_set_ptr->ref_pic_qp_array[1][0]);
1200 0 : if (ref_qp > 0) {
1201 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1202 : (uint32_t)ref_qp - 1,
1203 : picture_control_set_ptr->picture_qp,
1204 : picture_control_set_ptr->picture_qp);
1205 : }
1206 : }
1207 : // limiting the QP between min Qp allowed and max Qp allowed
1208 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
1209 : sequence_control_set_ptr->static_config.min_qp_allowed,
1210 : sequence_control_set_ptr->static_config.max_qp_allowed,
1211 : picture_control_set_ptr->picture_qp);
1212 :
1213 0 : rate_control_layer_ptr->delta_qp_fraction = CLIP3(-RC_PRECISION_OFFSET, RC_PRECISION_OFFSET, -((int64_t)temp_qp - (int64_t)(picture_control_set_ptr->picture_qp << RC_PRECISION)));
1214 :
1215 0 : if (picture_control_set_ptr->parent_pcs_ptr->sad_me == rate_control_layer_ptr->previous_frame_distortion_me &&
1216 0 : (rate_control_layer_ptr->previous_frame_distortion_me != 0))
1217 0 : rate_control_layer_ptr->same_distortion_count++;
1218 : else
1219 0 : rate_control_layer_ptr->same_distortion_count = 0;
1220 : }
1221 :
1222 0 : rate_control_layer_ptr->previous_c_coeff = rate_control_layer_ptr->c_coeff;
1223 0 : rate_control_layer_ptr->previous_k_coeff = rate_control_layer_ptr->k_coeff;
1224 0 : rate_control_layer_ptr->previous_calculated_frame_qp = rate_control_layer_ptr->calculated_frame_qp;
1225 0 : }
1226 :
1227 0 : void frame_level_rc_feedback_picture_vbr(
1228 : PictureParentControlSet *parentpicture_control_set_ptr,
1229 : SequenceControlSet *sequence_control_set_ptr,
1230 : RateControlContext *context_ptr)
1231 : {
1232 : RateControlLayerContext *rate_control_layer_temp_ptr;
1233 : RateControlIntervalParamContext *rate_control_param_ptr;
1234 : RateControlLayerContext *rate_control_layer_ptr;
1235 : // SB Loop variables
1236 : uint32_t slice_num;
1237 : uint64_t previous_frame_bit_actual;
1238 :
1239 0 : if (sequence_control_set_ptr->intra_period_length == -1)
1240 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
1241 : else {
1242 0 : uint32_t interval_index_temp = 0;
1243 0 : while ((!(parentpicture_control_set_ptr->picture_number >= context_ptr->rate_control_param_queue[interval_index_temp]->first_poc &&
1244 0 : parentpicture_control_set_ptr->picture_number <= context_ptr->rate_control_param_queue[interval_index_temp]->last_poc)) &&
1245 : (interval_index_temp < PARALLEL_GOP_MAX_NUMBER)) {
1246 0 : interval_index_temp++;
1247 : }
1248 0 : CHECK_REPORT_ERROR(
1249 : interval_index_temp != PARALLEL_GOP_MAX_NUMBER,
1250 : sequence_control_set_ptr->encode_context_ptr->app_callback_ptr,
1251 : EB_ENC_RC_ERROR2);
1252 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[interval_index_temp];
1253 : }
1254 :
1255 0 : rate_control_layer_ptr = rate_control_param_ptr->rate_control_layer_array[parentpicture_control_set_ptr->temporal_layer_index];
1256 :
1257 0 : rate_control_layer_ptr->max_qp = 0;
1258 :
1259 0 : rate_control_layer_ptr->feedback_arrived = EB_TRUE;
1260 0 : rate_control_layer_ptr->max_qp = MAX(rate_control_layer_ptr->max_qp, parentpicture_control_set_ptr->picture_qp);
1261 :
1262 0 : rate_control_layer_ptr->previous_frame_qp = parentpicture_control_set_ptr->picture_qp;
1263 0 : rate_control_layer_ptr->previous_frame_bit_actual = parentpicture_control_set_ptr->total_num_bits;
1264 0 : if (parentpicture_control_set_ptr->quantized_coeff_num_bits == 0)
1265 0 : parentpicture_control_set_ptr->quantized_coeff_num_bits = 1;
1266 0 : rate_control_layer_ptr->previous_framequantized_coeff_bit_actual = parentpicture_control_set_ptr->quantized_coeff_num_bits;
1267 :
1268 : // Setting Critical states for adjusting the averaging weights on C and K
1269 0 : if ((parentpicture_control_set_ptr->sad_me > (3 * rate_control_layer_ptr->previous_frame_distortion_me) >> 1) &&
1270 0 : (rate_control_layer_ptr->previous_frame_distortion_me != 0)) {
1271 0 : rate_control_layer_ptr->critical_states = 3;
1272 : }
1273 0 : else if (rate_control_layer_ptr->critical_states)
1274 0 : rate_control_layer_ptr->critical_states--;
1275 : else
1276 0 : rate_control_layer_ptr->critical_states = 0;
1277 0 : if (parentpicture_control_set_ptr->slice_type != I_SLICE) {
1278 : // Updating c_coeff
1279 0 : rate_control_layer_ptr->c_coeff = (((int64_t)rate_control_layer_ptr->previous_frame_bit_actual - (int64_t)rate_control_layer_ptr->previous_framequantized_coeff_bit_actual) << (2 * RC_PRECISION))
1280 0 : / rate_control_layer_ptr->area_in_pixel;
1281 0 : rate_control_layer_ptr->c_coeff = MAX(rate_control_layer_ptr->c_coeff, 1);
1282 :
1283 : // Updating k_coeff
1284 0 : if ((parentpicture_control_set_ptr->sad_me + RC_PRECISION_OFFSET) >> RC_PRECISION > 5) {
1285 : {
1286 : uint64_t test1, test2, test3;
1287 0 : test1 = rate_control_layer_ptr->previous_framequantized_coeff_bit_actual*(two_to_power_qp_over_three[parentpicture_control_set_ptr->picture_qp]);
1288 0 : test2 = MAX(parentpicture_control_set_ptr->sad_me / rate_control_layer_ptr->area_in_pixel, 1);
1289 0 : test3 = test1 * 65536 / test2 * 65536 / parentpicture_control_set_ptr->sad_me;
1290 :
1291 0 : rate_control_layer_ptr->k_coeff = test3;
1292 : }
1293 : }
1294 :
1295 0 : if (rate_control_layer_ptr->critical_states) {
1296 0 : rate_control_layer_ptr->k_coeff = (8 * rate_control_layer_ptr->k_coeff + 8 * rate_control_layer_ptr->previous_k_coeff + 8) >> 4;
1297 0 : rate_control_layer_ptr->c_coeff = (8 * rate_control_layer_ptr->c_coeff + 8 * rate_control_layer_ptr->previous_c_coeff + 8) >> 4;
1298 : }
1299 : else {
1300 0 : rate_control_layer_ptr->k_coeff = (rate_control_layer_ptr->coeff_averaging_weight1*rate_control_layer_ptr->k_coeff + rate_control_layer_ptr->coeff_averaging_weight2*rate_control_layer_ptr->previous_k_coeff + 8) >> 4;
1301 0 : rate_control_layer_ptr->c_coeff = (rate_control_layer_ptr->coeff_averaging_weight1*rate_control_layer_ptr->c_coeff + rate_control_layer_ptr->coeff_averaging_weight2*rate_control_layer_ptr->previous_c_coeff + 8) >> 4;
1302 : }
1303 0 : rate_control_layer_ptr->k_coeff = MIN(rate_control_layer_ptr->k_coeff, rate_control_layer_ptr->previous_k_coeff * 4);
1304 0 : rate_control_layer_ptr->c_coeff = MIN(rate_control_layer_ptr->c_coeff, rate_control_layer_ptr->previous_c_coeff * 4);
1305 0 : if (parentpicture_control_set_ptr->slice_type != I_SLICE)
1306 0 : rate_control_layer_ptr->previous_frame_distortion_me = parentpicture_control_set_ptr->sad_me;
1307 : else
1308 0 : rate_control_layer_ptr->previous_frame_distortion_me = 0;
1309 : }
1310 :
1311 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
1312 0 : if (parentpicture_control_set_ptr->slice_type == I_SLICE) {
1313 0 : if (parentpicture_control_set_ptr->total_num_bits < parentpicture_control_set_ptr->target_bits_best_pred_qp << 1)
1314 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 2) >> 2;
1315 0 : else if (parentpicture_control_set_ptr->total_num_bits > parentpicture_control_set_ptr->target_bits_best_pred_qp << 2)
1316 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 4 + 2) >> 2;
1317 0 : else if (parentpicture_control_set_ptr->total_num_bits > parentpicture_control_set_ptr->target_bits_best_pred_qp << 1)
1318 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 2 + 2) >> 2;
1319 : }
1320 : }
1321 :
1322 : {
1323 0 : uint64_t previous_frame_ec_bits = 0;
1324 0 : EbBool picture_min_qp_allowed = EB_TRUE;
1325 0 : rate_control_layer_ptr->previous_frame_average_qp = 0;
1326 0 : rate_control_layer_ptr->previous_frame_average_qp += rate_control_layer_ptr->previous_frame_qp;
1327 0 : previous_frame_ec_bits += rate_control_layer_ptr->previous_frame_bit_actual;
1328 0 : if (rate_control_layer_ptr->same_distortion_count == 0 ||
1329 0 : parentpicture_control_set_ptr->picture_qp != sequence_control_set_ptr->static_config.min_qp_allowed) {
1330 0 : picture_min_qp_allowed = EB_FALSE;
1331 : }
1332 0 : if (picture_min_qp_allowed)
1333 0 : rate_control_layer_ptr->frame_same_distortion_min_qp_count++;
1334 : else
1335 0 : rate_control_layer_ptr->frame_same_distortion_min_qp_count = 0;
1336 :
1337 0 : rate_control_layer_ptr->previous_ec_bits = previous_frame_ec_bits;
1338 0 : previous_frame_bit_actual = parentpicture_control_set_ptr->total_num_bits;
1339 0 : if (parentpicture_control_set_ptr->first_frame_in_temporal_layer)
1340 0 : rate_control_layer_ptr->dif_total_and_ec_bits = (previous_frame_bit_actual - previous_frame_ec_bits);
1341 : else
1342 0 : rate_control_layer_ptr->dif_total_and_ec_bits = ((previous_frame_bit_actual - previous_frame_ec_bits) + rate_control_layer_ptr->dif_total_and_ec_bits) >> 1;
1343 : // update bitrate of different layers in the interval based on the rate of the I frame
1344 0 : if (parentpicture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc &&
1345 0 : (parentpicture_control_set_ptr->slice_type == I_SLICE) &&
1346 0 : sequence_control_set_ptr->static_config.intra_period_length != -1) {
1347 : uint32_t temporal_layer_idex;
1348 : uint64_t target_bit_rate;
1349 : uint64_t channel_bit_rate;
1350 0 : uint64_t sum_bits_per_sw = 0;
1351 : #if ADAPTIVE_PERCENTAGE
1352 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
1353 0 : if (parentpicture_control_set_ptr->tables_updated && parentpicture_control_set_ptr->percentage_updated) {
1354 0 : parentpicture_control_set_ptr->bits_per_sw_per_layer[0] =
1355 0 : (uint64_t)MAX((int64_t)parentpicture_control_set_ptr->bits_per_sw_per_layer[0] + (int64_t)parentpicture_control_set_ptr->total_num_bits - (int64_t)parentpicture_control_set_ptr->target_bits_best_pred_qp, 1);
1356 : }
1357 : }
1358 : #endif
1359 :
1360 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0 && sequence_control_set_ptr->intra_period_length != -1) {
1361 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++)
1362 0 : sum_bits_per_sw += parentpicture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_idex];
1363 : }
1364 :
1365 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
1366 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
1367 :
1368 0 : target_bit_rate = (uint64_t)((int64_t)parentpicture_control_set_ptr->target_bit_rate -
1369 0 : MIN((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4, (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION))
1370 0 : *rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_idex] / 100;
1371 :
1372 : #if ADAPTIVE_PERCENTAGE
1373 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0 && sequence_control_set_ptr->intra_period_length != -1) {
1374 0 : target_bit_rate = (uint64_t)((int64_t)parentpicture_control_set_ptr->target_bit_rate -
1375 0 : MIN((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4, (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION))
1376 0 : *parentpicture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_idex] / sum_bits_per_sw;
1377 : }
1378 : #endif
1379 : // update this based on temporal layers
1380 0 : if (temporal_layer_idex == 0)
1381 0 : channel_bit_rate = (((target_bit_rate << (2 * RC_PRECISION)) / MAX(1, rate_control_layer_temp_ptr->frame_rate - (1 * context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)))) + RC_PRECISION_OFFSET) >> RC_PRECISION;
1382 : else
1383 0 : channel_bit_rate = (((target_bit_rate << (2 * RC_PRECISION)) / rate_control_layer_temp_ptr->frame_rate) + RC_PRECISION_OFFSET) >> RC_PRECISION;
1384 0 : channel_bit_rate = (uint64_t)MAX((int64_t)1, (int64_t)channel_bit_rate);
1385 0 : rate_control_layer_temp_ptr->ec_bit_constraint = channel_bit_rate;
1386 :
1387 0 : slice_num = 1;
1388 0 : rate_control_layer_temp_ptr->ec_bit_constraint -= SLICE_HEADER_BITS_NUM * slice_num;
1389 :
1390 0 : rate_control_layer_temp_ptr->previous_bit_constraint = channel_bit_rate;
1391 0 : rate_control_layer_temp_ptr->bit_constraint = channel_bit_rate;
1392 0 : rate_control_layer_temp_ptr->channel_bit_rate = channel_bit_rate;
1393 : }
1394 0 : if ((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4 < (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION) {
1395 0 : rate_control_param_ptr->previous_virtual_buffer_level += (int64_t)((parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION) - (int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4;
1396 0 : context_ptr->extra_bits_gen -= (int64_t)((parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION) - (int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4;
1397 : }
1398 : }
1399 :
1400 0 : if (previous_frame_bit_actual) {
1401 : uint64_t bit_changes_rate;
1402 : // Updating virtual buffer level and it can be negative
1403 0 : if ((parentpicture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) &&
1404 0 : (parentpicture_control_set_ptr->slice_type == I_SLICE) &&
1405 0 : (rate_control_param_ptr->last_gop == EB_FALSE) &&
1406 0 : sequence_control_set_ptr->static_config.intra_period_length != -1) {
1407 0 : rate_control_param_ptr->virtual_buffer_level =
1408 0 : (int64_t)rate_control_param_ptr->previous_virtual_buffer_level;
1409 : }
1410 : else {
1411 0 : rate_control_param_ptr->virtual_buffer_level =
1412 0 : (int64_t)rate_control_param_ptr->previous_virtual_buffer_level +
1413 0 : (int64_t)previous_frame_bit_actual - (int64_t)rate_control_layer_ptr->channel_bit_rate;
1414 0 : context_ptr->extra_bits_gen -= (int64_t)previous_frame_bit_actual - (int64_t)rate_control_layer_ptr->channel_bit_rate;
1415 : }
1416 0 : if (parentpicture_control_set_ptr->hierarchical_levels > 1 && rate_control_layer_ptr->frame_same_distortion_min_qp_count > 10) {
1417 0 : rate_control_layer_ptr->previous_bit_constraint = (int64_t)rate_control_layer_ptr->channel_bit_rate;
1418 0 : rate_control_param_ptr->virtual_buffer_level = ((int64_t)context_ptr->virtual_buffer_size >> 1);
1419 : }
1420 : // Updating bit difference
1421 0 : rate_control_layer_ptr->bit_diff = (int64_t)rate_control_param_ptr->virtual_buffer_level
1422 : //- ((int64_t)context_ptr->virtual_buffer_size>>1);
1423 0 : - ((int64_t)rate_control_layer_ptr->channel_bit_rate >> 1);
1424 :
1425 : // Limit the bit difference
1426 0 : rate_control_layer_ptr->bit_diff = CLIP3(-(int64_t)(rate_control_layer_ptr->channel_bit_rate), (int64_t)(rate_control_layer_ptr->channel_bit_rate >> 1), rate_control_layer_ptr->bit_diff);
1427 0 : bit_changes_rate = rate_control_layer_ptr->frame_rate;
1428 :
1429 : // Updating bit Constraint
1430 0 : rate_control_layer_ptr->bit_constraint = MAX((int64_t)rate_control_layer_ptr->previous_bit_constraint - ((rate_control_layer_ptr->bit_diff << RC_PRECISION) / ((int64_t)bit_changes_rate)), 1);
1431 :
1432 : // Limiting the bit_constraint
1433 0 : if (parentpicture_control_set_ptr->temporal_layer_index == 0) {
1434 0 : rate_control_layer_ptr->bit_constraint = CLIP3(rate_control_layer_ptr->channel_bit_rate >> 2,
1435 : rate_control_layer_ptr->channel_bit_rate * 200 / 100,
1436 : rate_control_layer_ptr->bit_constraint);
1437 : }
1438 : else {
1439 0 : rate_control_layer_ptr->bit_constraint = CLIP3(rate_control_layer_ptr->channel_bit_rate >> 1,
1440 : rate_control_layer_ptr->channel_bit_rate * 200 / 100,
1441 : rate_control_layer_ptr->bit_constraint);
1442 : }
1443 0 : rate_control_layer_ptr->ec_bit_constraint = (uint64_t)MAX((int64_t)rate_control_layer_ptr->bit_constraint - (int64_t)rate_control_layer_ptr->dif_total_and_ec_bits, 1);
1444 0 : rate_control_param_ptr->previous_virtual_buffer_level = rate_control_param_ptr->virtual_buffer_level;
1445 0 : rate_control_layer_ptr->previous_bit_constraint = rate_control_layer_ptr->bit_constraint;
1446 : }
1447 :
1448 0 : rate_control_param_ptr->processed_frames_number++;
1449 0 : rate_control_param_ptr->in_use = EB_TRUE;
1450 : // check if all the frames in the interval have arrived
1451 0 : if (rate_control_param_ptr->processed_frames_number == (rate_control_param_ptr->last_poc - rate_control_param_ptr->first_poc + 1) &&
1452 0 : sequence_control_set_ptr->intra_period_length != -1) {
1453 : uint32_t temporal_index;
1454 : int64_t extra_bits;
1455 0 : rate_control_param_ptr->first_poc += PARALLEL_GOP_MAX_NUMBER * (uint32_t)(sequence_control_set_ptr->intra_period_length + 1);
1456 0 : rate_control_param_ptr->last_poc += PARALLEL_GOP_MAX_NUMBER * (uint32_t)(sequence_control_set_ptr->intra_period_length + 1);
1457 0 : rate_control_param_ptr->processed_frames_number = 0;
1458 0 : rate_control_param_ptr->extra_ap_bit_ratio_i = 0;
1459 0 : rate_control_param_ptr->in_use = EB_FALSE;
1460 0 : rate_control_param_ptr->was_used = EB_TRUE;
1461 0 : rate_control_param_ptr->last_gop = EB_FALSE;
1462 0 : rate_control_param_ptr->first_pic_actual_qp_assigned = EB_FALSE;
1463 0 : for (temporal_index = 0; temporal_index < EB_MAX_TEMPORAL_LAYERS; temporal_index++) {
1464 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->first_frame = 1;
1465 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->first_non_intra_frame = 1;
1466 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->feedback_arrived = EB_FALSE;
1467 : }
1468 0 : extra_bits = ((int64_t)context_ptr->virtual_buffer_size >> 1) - (int64_t)rate_control_param_ptr->virtual_buffer_level;
1469 :
1470 0 : rate_control_param_ptr->virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
1471 0 : context_ptr->extra_bits += extra_bits;
1472 : }
1473 : // Allocate the extra_bits among other GOPs
1474 0 : if ((parentpicture_control_set_ptr->temporal_layer_index <= 2) &&
1475 0 : ((context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size >> 8)) ||
1476 0 : (context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size >> 8)))) {
1477 : uint32_t interval_index_temp, interval_in_use_count;
1478 : int64_t extra_bits_per_gop;
1479 0 : int64_t extra_bits = context_ptr->extra_bits;
1480 : int32_t clip_coef1, clip_coef2;
1481 0 : if (parentpicture_control_set_ptr->end_of_sequence_region) {
1482 0 : clip_coef1 = -1;
1483 0 : clip_coef2 = -1;
1484 : }
1485 : else {
1486 0 : if (context_ptr->extra_bits > (int64_t)(context_ptr->virtual_buffer_size << 3) ||
1487 0 : context_ptr->extra_bits < -(int64_t)(context_ptr->virtual_buffer_size << 3)) {
1488 0 : clip_coef1 = 0;
1489 0 : clip_coef2 = 0;
1490 : }
1491 : else {
1492 0 : clip_coef1 = 2;
1493 0 : clip_coef2 = 4;
1494 : }
1495 : }
1496 :
1497 0 : interval_in_use_count = 0;
1498 :
1499 0 : if (extra_bits > 0) {
1500 : // Extra bits to be distributed
1501 : // Distribute it among those that are consuming more
1502 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1503 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1504 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level > ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1505 0 : interval_in_use_count++;
1506 : }
1507 : }
1508 : // Distribute the rate among them
1509 0 : if (interval_in_use_count) {
1510 0 : extra_bits_per_gop = extra_bits / interval_in_use_count;
1511 0 : if (clip_coef1 > 0)
1512 0 : extra_bits_per_gop = CLIP3(
1513 : -(int64_t)context_ptr->virtual_buffer_size >> clip_coef1,
1514 : (int64_t)context_ptr->virtual_buffer_size >> clip_coef1,
1515 : extra_bits_per_gop);
1516 : else
1517 0 : extra_bits_per_gop = CLIP3(
1518 : -(int64_t)context_ptr->virtual_buffer_size << (-clip_coef1),
1519 : (int64_t)context_ptr->virtual_buffer_size << (-clip_coef1),
1520 : extra_bits_per_gop);
1521 :
1522 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1523 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1524 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level > ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1525 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level -= extra_bits_per_gop;
1526 0 : context_ptr->rate_control_param_queue[interval_index_temp]->previous_virtual_buffer_level -= extra_bits_per_gop;
1527 0 : context_ptr->extra_bits -= extra_bits_per_gop;
1528 : }
1529 : }
1530 : }
1531 : // if no interval with more consuming was found, allocate it to ones with consuming less
1532 : else {
1533 0 : interval_in_use_count = 0;
1534 : // Distribute it among those that are consuming less
1535 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1536 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1537 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level <= ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1538 0 : interval_in_use_count++;
1539 : }
1540 : }
1541 0 : if (interval_in_use_count) {
1542 0 : extra_bits_per_gop = extra_bits / interval_in_use_count;
1543 0 : if (clip_coef2 > 0)
1544 0 : extra_bits_per_gop = CLIP3(
1545 : -(int64_t)context_ptr->virtual_buffer_size >> clip_coef2,
1546 : (int64_t)context_ptr->virtual_buffer_size >> clip_coef2,
1547 : extra_bits_per_gop);
1548 : else
1549 0 : extra_bits_per_gop = CLIP3(
1550 : -(int64_t)context_ptr->virtual_buffer_size << (-clip_coef2),
1551 : (int64_t)context_ptr->virtual_buffer_size << (-clip_coef2),
1552 : extra_bits_per_gop);
1553 : // Distribute the rate among them
1554 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1555 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1556 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level <= ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1557 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level -= extra_bits_per_gop;
1558 0 : context_ptr->rate_control_param_queue[interval_index_temp]->previous_virtual_buffer_level -= extra_bits_per_gop;
1559 0 : context_ptr->extra_bits -= extra_bits_per_gop;
1560 : }
1561 : }
1562 : }
1563 : }
1564 : }
1565 : else {
1566 : // Distribute it among those that are consuming less
1567 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1568 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1569 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level < ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1570 0 : interval_in_use_count++;
1571 : }
1572 : }
1573 0 : if (interval_in_use_count) {
1574 0 : extra_bits_per_gop = extra_bits / interval_in_use_count;
1575 0 : if (clip_coef1 > 0)
1576 0 : extra_bits_per_gop = CLIP3(
1577 : -(int64_t)context_ptr->virtual_buffer_size >> clip_coef1,
1578 : (int64_t)context_ptr->virtual_buffer_size >> clip_coef1,
1579 : extra_bits_per_gop);
1580 : else
1581 0 : extra_bits_per_gop = CLIP3(
1582 : -(int64_t)context_ptr->virtual_buffer_size << (-clip_coef1),
1583 : (int64_t)context_ptr->virtual_buffer_size << (-clip_coef1),
1584 : extra_bits_per_gop);
1585 : // Distribute the rate among them
1586 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1587 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1588 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level < ((int64_t)context_ptr->virtual_buffer_size >> 1)) {
1589 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level -= extra_bits_per_gop;
1590 0 : context_ptr->rate_control_param_queue[interval_index_temp]->previous_virtual_buffer_level -= extra_bits_per_gop;
1591 0 : context_ptr->extra_bits -= extra_bits_per_gop;
1592 : }
1593 : }
1594 : }
1595 : // if no interval with less consuming was found, allocate it to ones with consuming more
1596 : else {
1597 0 : interval_in_use_count = 0;
1598 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1599 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1600 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level < (int64_t)(context_ptr->virtual_buffer_size)) {
1601 0 : interval_in_use_count++;
1602 : }
1603 : }
1604 0 : if (interval_in_use_count) {
1605 0 : extra_bits_per_gop = extra_bits / interval_in_use_count;
1606 0 : if (clip_coef2 > 0)
1607 0 : extra_bits_per_gop = CLIP3(
1608 : -(int64_t)context_ptr->virtual_buffer_size >> clip_coef2,
1609 : (int64_t)context_ptr->virtual_buffer_size >> clip_coef2,
1610 : extra_bits_per_gop);
1611 : else
1612 0 : extra_bits_per_gop = CLIP3(
1613 : -(int64_t)context_ptr->virtual_buffer_size << (-clip_coef2),
1614 : (int64_t)context_ptr->virtual_buffer_size << (-clip_coef2),
1615 : extra_bits_per_gop);
1616 : // Distribute the rate among them
1617 0 : for (interval_index_temp = 0; interval_index_temp < PARALLEL_GOP_MAX_NUMBER; interval_index_temp++) {
1618 0 : if (context_ptr->rate_control_param_queue[interval_index_temp]->in_use &&
1619 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level < (int64_t)(context_ptr->virtual_buffer_size)) {
1620 0 : context_ptr->rate_control_param_queue[interval_index_temp]->virtual_buffer_level -= extra_bits_per_gop;
1621 0 : context_ptr->rate_control_param_queue[interval_index_temp]->previous_virtual_buffer_level -= extra_bits_per_gop;
1622 0 : context_ptr->extra_bits -= extra_bits_per_gop;
1623 : }
1624 : }
1625 : }
1626 : }
1627 : }
1628 : }
1629 : }
1630 :
1631 : #if RC_PRINTS
1632 : if (parentpicture_control_set_ptr->temporal_layer_index == 0)
1633 : {
1634 : SVT_LOG("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.0f\t%.0f\t%.0f\t%.0f\t%d\t%d\n",
1635 : (int)parentpicture_control_set_ptr->slice_type,
1636 : (int)parentpicture_control_set_ptr->picture_number,
1637 : (int)parentpicture_control_set_ptr->temporal_layer_index,
1638 : (int)parentpicture_control_set_ptr->picture_qp, (int)parentpicture_control_set_ptr->calculated_qp, (int)parentpicture_control_set_ptr->best_pred_qp,
1639 : (int)previous_frame_bit_actual,
1640 : (int)parentpicture_control_set_ptr->target_bits_best_pred_qp,
1641 : (int)parentpicture_control_set_ptr->target_bits_rc,
1642 : (int)rate_control_layer_ptr->channel_bit_rate,
1643 : (int)rate_control_layer_ptr->bit_constraint,
1644 : (double)rate_control_layer_ptr->c_coeff,
1645 : (double)rate_control_layer_ptr->k_coeff,
1646 : (double)parentpicture_control_set_ptr->sad_me,
1647 : (double)context_ptr->extra_bits_gen,
1648 : (int)rate_control_param_ptr->virtual_buffer_level,
1649 : (int)context_ptr->extra_bits);
1650 : }
1651 : #endif
1652 0 : }
1653 0 : void high_level_rc_input_picture_cvbr(
1654 : PictureParentControlSet *picture_control_set_ptr,
1655 : SequenceControlSet *sequence_control_set_ptr,
1656 : EncodeContext *encode_context_ptr,
1657 : RateControlContext *context_ptr,
1658 : HighLevelRateControlContext *high_level_rate_control_ptr)
1659 : {
1660 0 : EbBool end_of_sequence_flag = EB_TRUE;
1661 :
1662 : HlRateControlHistogramEntry *hl_rate_control_histogram_ptr_temp;
1663 : // Queue variables
1664 : uint32_t queue_entry_index_temp;
1665 : uint32_t queue_entry_index_temp2;
1666 : int64_t queue_entry_index_head_temp;
1667 :
1668 : uint64_t min_la_bit_distance;
1669 : uint32_t selected_ref_qp_table_index;
1670 : uint32_t selected_ref_qp;
1671 : #if RC_UPDATE_TARGET_RATE
1672 : uint32_t selected_org_ref_qp;
1673 : #endif
1674 0 : uint32_t previous_selected_ref_qp = encode_context_ptr->previous_selected_ref_qp;
1675 0 : uint64_t max_coded_poc = encode_context_ptr->max_coded_poc;
1676 0 : uint32_t max_coded_poc_selected_ref_qp = encode_context_ptr->max_coded_poc_selected_ref_qp;
1677 :
1678 : uint32_t ref_qp_index;
1679 : uint32_t ref_qp_index_temp;
1680 : uint32_t ref_qp_table_index;
1681 :
1682 : uint32_t area_in_pixel;
1683 : uint32_t num_of_full_sbs;
1684 : uint32_t qp_search_min;
1685 : uint32_t qp_search_max;
1686 0 : int32_t qp_step = 1;
1687 : EbBool best_qp_found;
1688 : uint32_t temporal_layer_index;
1689 : EbBool tables_updated;
1690 :
1691 0 : uint64_t bit_constraint_per_sw = 0;
1692 :
1693 : RateControlTables *rate_control_tables_ptr;
1694 : EbBitNumber *sad_bits_array_ptr;
1695 : EbBitNumber *intra_sad_bits_array_ptr;
1696 : uint32_t pred_bits_ref_qp;
1697 0 : int delta_qp = 0;
1698 :
1699 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
1700 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = 0;
1701 0 : picture_control_set_ptr->total_bits_per_gop = 0;
1702 :
1703 0 : area_in_pixel = sequence_control_set_ptr->seq_header.max_frame_width * sequence_control_set_ptr->seq_header.max_frame_height;;
1704 :
1705 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->rate_table_update_mutex);
1706 :
1707 0 : tables_updated = sequence_control_set_ptr->encode_context_ptr->rate_control_tables_array_updated;
1708 0 : picture_control_set_ptr->percentage_updated = EB_FALSE;
1709 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
1710 : // Increamenting the head of the hl_rate_control_historgram_queue and clean up the entores
1711 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]);
1712 :
1713 0 : while ((hl_rate_control_histogram_ptr_temp->life_count == 0) && hl_rate_control_histogram_ptr_temp->passed_to_hlrc) {
1714 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
1715 : // Reset the Reorder Queue Entry
1716 0 : hl_rate_control_histogram_ptr_temp->picture_number += INITIAL_RATE_CONTROL_REORDER_QUEUE_MAX_DEPTH;
1717 0 : hl_rate_control_histogram_ptr_temp->life_count = -1;
1718 0 : hl_rate_control_histogram_ptr_temp->passed_to_hlrc = EB_FALSE;
1719 0 : hl_rate_control_histogram_ptr_temp->is_coded = EB_FALSE;
1720 0 : hl_rate_control_histogram_ptr_temp->total_num_bits_coded = 0;
1721 :
1722 : // Increment the Reorder Queue head Ptr
1723 0 : encode_context_ptr->hl_rate_control_historgram_queue_head_index =
1724 0 : (encode_context_ptr->hl_rate_control_historgram_queue_head_index == HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? 0 : encode_context_ptr->hl_rate_control_historgram_queue_head_index + 1;
1725 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
1726 0 : hl_rate_control_histogram_ptr_temp = encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index];
1727 : }
1728 : // For the case that number of frames in the sliding window is less than size of the look ahead or intra Refresh. i.e. end of sequence
1729 0 : if ((picture_control_set_ptr->frames_in_sw < MIN(sequence_control_set_ptr->static_config.look_ahead_distance + 1, (uint32_t)sequence_control_set_ptr->intra_period_length + 1))) {
1730 0 : selected_ref_qp = max_coded_poc_selected_ref_qp;
1731 :
1732 : // Update the QP for the sliding window based on the status of RC
1733 0 : if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 3)))
1734 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 2, 0);
1735 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 2)))
1736 0 : selected_ref_qp = (uint32_t)MAX((int32_t)selected_ref_qp - 1, 0);
1737 0 : if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 2)))
1738 0 : selected_ref_qp += 2;
1739 0 : else if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 1)))
1740 0 : selected_ref_qp += 1;
1741 0 : if ((picture_control_set_ptr->frames_in_sw < (uint32_t)(sequence_control_set_ptr->intra_period_length + 1)) &&
1742 0 : (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0)) {
1743 0 : selected_ref_qp++;
1744 : }
1745 :
1746 0 : selected_ref_qp = (uint32_t)CLIP3(
1747 : sequence_control_set_ptr->static_config.min_qp_allowed,
1748 : sequence_control_set_ptr->static_config.max_qp_allowed,
1749 : selected_ref_qp);
1750 :
1751 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
1752 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
1753 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
1754 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1755 : (queue_entry_index_head_temp < 0) ?
1756 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1757 : queue_entry_index_head_temp;
1758 :
1759 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
1760 : {
1761 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp]);
1762 :
1763 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
1764 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
1765 : else
1766 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][selected_ref_qp];
1767 :
1768 0 : ref_qp_index_temp = (uint32_t)CLIP3(
1769 : sequence_control_set_ptr->static_config.min_qp_allowed,
1770 : sequence_control_set_ptr->static_config.max_qp_allowed,
1771 : ref_qp_index_temp);
1772 :
1773 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = 0;
1774 0 : rate_control_tables_ptr = &encode_context_ptr->rate_control_tables_array[ref_qp_index_temp];
1775 0 : sad_bits_array_ptr = rate_control_tables_ptr->sad_bits_array[hl_rate_control_histogram_ptr_temp->temporal_layer_index];
1776 0 : intra_sad_bits_array_ptr = rate_control_tables_ptr->intra_sad_bits_array[hl_rate_control_histogram_ptr_temp->temporal_layer_index];
1777 0 : pred_bits_ref_qp = 0;
1778 0 : num_of_full_sbs = 0;
1779 :
1780 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE) {
1781 : // Loop over block in the frame and calculated the predicted bits at reg QP
1782 : {
1783 : unsigned i;
1784 0 : uint32_t accum = 0;
1785 0 : for (i = 0; i < NUMBER_OF_INTRA_SAD_INTERVALS; ++i)
1786 0 : accum += (uint32_t)(hl_rate_control_histogram_ptr_temp->ois_distortion_histogram[i] * intra_sad_bits_array_ptr[i]);
1787 0 : pred_bits_ref_qp = accum;
1788 0 : num_of_full_sbs = hl_rate_control_histogram_ptr_temp->full_sb_count;
1789 : }
1790 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] += pred_bits_ref_qp;
1791 : }
1792 :
1793 : else {
1794 : {
1795 : unsigned i;
1796 0 : uint32_t accum = 0;
1797 0 : for (i = 0; i < NUMBER_OF_SAD_INTERVALS; ++i)
1798 0 : accum += (uint32_t)(hl_rate_control_histogram_ptr_temp->me_distortion_histogram[i] * sad_bits_array_ptr[i]);
1799 0 : pred_bits_ref_qp = accum;
1800 0 : num_of_full_sbs = hl_rate_control_histogram_ptr_temp->full_sb_count;
1801 : }
1802 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] += pred_bits_ref_qp;
1803 : }
1804 :
1805 : // Scale for in complete
1806 : // pred_bits_ref_qp is normalized based on the area because of the LCUs at the picture boundries
1807 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] * (uint64_t)area_in_pixel / (num_of_full_sbs << 12);
1808 :
1809 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
1810 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
1811 : }
1812 : }
1813 : else {
1814 : // Loop over the QPs and find the best QP
1815 0 : min_la_bit_distance = MAX_UNSIGNED_VALUE;
1816 0 : qp_search_min = (uint8_t)CLIP3(
1817 : sequence_control_set_ptr->static_config.min_qp_allowed,
1818 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
1819 : (uint32_t)MAX((int32_t)sequence_control_set_ptr->qp - 40, 0));
1820 :
1821 0 : qp_search_max = (uint8_t)CLIP3(
1822 : sequence_control_set_ptr->static_config.min_qp_allowed,
1823 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
1824 : sequence_control_set_ptr->qp + 40);
1825 :
1826 0 : for (ref_qp_table_index = qp_search_min; ref_qp_table_index < qp_search_max; ref_qp_table_index++)
1827 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_table_index] = 0;
1828 0 : bit_constraint_per_sw = high_level_rate_control_ptr->bit_constraint_per_sw * picture_control_set_ptr->frames_in_sw / (sequence_control_set_ptr->static_config.look_ahead_distance + 1);
1829 :
1830 : // Update the target rate for the sliding window based on the status of RC
1831 0 : if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size * 10)))
1832 0 : bit_constraint_per_sw = bit_constraint_per_sw * 130 / 100;
1833 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 3)))
1834 0 : bit_constraint_per_sw = bit_constraint_per_sw * 120 / 100;
1835 0 : else if ((context_ptr->extra_bits_gen > (int64_t)(context_ptr->virtual_buffer_size << 2)))
1836 0 : bit_constraint_per_sw = bit_constraint_per_sw * 110 / 100;
1837 0 : if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 3)))
1838 0 : bit_constraint_per_sw = bit_constraint_per_sw * 80 / 100;
1839 0 : else if ((context_ptr->extra_bits_gen < -(int64_t)(context_ptr->virtual_buffer_size << 2)))
1840 0 : bit_constraint_per_sw = bit_constraint_per_sw * 90 / 100;
1841 : // Loop over proper QPs and find the Predicted bits for that QP. Find the QP with the closest total predicted rate to target bits for the sliding window.
1842 0 : previous_selected_ref_qp = CLIP3(
1843 : qp_search_min + 1,
1844 : qp_search_max - 1,
1845 : previous_selected_ref_qp);
1846 0 : ref_qp_table_index = previous_selected_ref_qp;
1847 0 : ref_qp_index = ref_qp_table_index;
1848 0 : selected_ref_qp_table_index = ref_qp_table_index;
1849 0 : selected_ref_qp = selected_ref_qp_table_index;
1850 0 : if (sequence_control_set_ptr->intra_period_length != -1 && picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0 &&
1851 0 : (int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length) {
1852 0 : best_qp_found = EB_FALSE;
1853 0 : while (ref_qp_table_index >= qp_search_min && ref_qp_table_index <= qp_search_max && !best_qp_found) {
1854 0 : ref_qp_index = CLIP3(
1855 : sequence_control_set_ptr->static_config.min_qp_allowed,
1856 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
1857 : ref_qp_table_index);
1858 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] = 0;
1859 :
1860 : // Finding the predicted bits for each frame in the sliding window at the reference Qp(s)
1861 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
1862 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
1863 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
1864 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1865 : (queue_entry_index_head_temp < 0) ?
1866 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1867 : queue_entry_index_head_temp;
1868 :
1869 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
1870 : // This is set to false, so the last frame would go inside the loop
1871 0 : end_of_sequence_flag = EB_FALSE;
1872 :
1873 0 : while (!end_of_sequence_flag &&
1874 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->static_config.look_ahead_distance) {
1875 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
1876 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
1877 :
1878 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
1879 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[ref_qp_index];
1880 : else
1881 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][ref_qp_index];
1882 :
1883 0 : ref_qp_index_temp = (uint32_t)CLIP3(
1884 : sequence_control_set_ptr->static_config.min_qp_allowed,
1885 : sequence_control_set_ptr->static_config.max_qp_allowed,
1886 : ref_qp_index_temp);
1887 :
1888 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = 0;
1889 :
1890 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
1891 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
1892 0 : hl_rate_control_histogram_ptr_temp->life_count--;
1893 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
1894 : }
1895 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = predict_bits(
1896 : encode_context_ptr,
1897 : hl_rate_control_histogram_ptr_temp,
1898 : ref_qp_index_temp,
1899 : area_in_pixel);
1900 :
1901 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
1902 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
1903 0 : if (queue_entry_index_head_temp == queue_entry_index_temp2)
1904 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
1905 :
1906 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
1907 0 : queue_entry_index_temp++;
1908 : }
1909 :
1910 0 : if (min_la_bit_distance >= (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw)) {
1911 0 : min_la_bit_distance = (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw);
1912 0 : selected_ref_qp_table_index = ref_qp_table_index;
1913 0 : selected_ref_qp = ref_qp_index;
1914 : }
1915 : else
1916 0 : best_qp_found = EB_TRUE;
1917 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
1918 0 : if (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] > bit_constraint_per_sw)
1919 0 : qp_step = +1;
1920 : else
1921 0 : qp_step = -1;
1922 : }
1923 0 : ref_qp_table_index = (uint32_t)(ref_qp_table_index + qp_step);
1924 : }
1925 :
1926 0 : if (ref_qp_index == sequence_control_set_ptr->static_config.max_qp_allowed && high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] > bit_constraint_per_sw) {
1927 0 : delta_qp = (int)((high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - bit_constraint_per_sw) * 100 / (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index - 1] - high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index]));
1928 0 : delta_qp = (delta_qp + 50) / 100;
1929 : }
1930 : }
1931 : }
1932 : #if RC_UPDATE_TARGET_RATE
1933 0 : selected_org_ref_qp = selected_ref_qp;
1934 0 : if (sequence_control_set_ptr->intra_period_length != -1 && picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0 &&
1935 0 : (int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length) {
1936 0 : if (picture_control_set_ptr->picture_number > 0)
1937 0 : picture_control_set_ptr->intra_selected_org_qp = (uint8_t)selected_ref_qp;
1938 0 : ref_qp_index = selected_ref_qp;
1939 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] = 0;
1940 :
1941 0 : if (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] == 0) {
1942 : // Finding the predicted bits for each frame in the sliding window at the reference Qp(s)
1943 : //queue_entry_index_temp = encode_context_ptr->hl_rate_control_historgram_queue_head_index;
1944 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
1945 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
1946 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
1947 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1948 : (queue_entry_index_head_temp < 0) ?
1949 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
1950 : queue_entry_index_head_temp;
1951 :
1952 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
1953 :
1954 : // This is set to false, so the last frame would go inside the loop
1955 0 : end_of_sequence_flag = EB_FALSE;
1956 :
1957 0 : while (!end_of_sequence_flag &&
1958 : //queue_entry_index_temp <= encode_context_ptr->hl_rate_control_historgram_queue_head_index+sequence_control_set_ptr->static_config.look_ahead_distance){
1959 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->static_config.look_ahead_distance) {
1960 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
1961 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
1962 :
1963 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
1964 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[ref_qp_index];
1965 : else
1966 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][ref_qp_index];
1967 :
1968 0 : ref_qp_index_temp = (uint32_t)CLIP3(
1969 : sequence_control_set_ptr->static_config.min_qp_allowed,
1970 : sequence_control_set_ptr->static_config.max_qp_allowed,
1971 : ref_qp_index_temp);
1972 :
1973 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = predict_bits(
1974 : encode_context_ptr,
1975 : hl_rate_control_histogram_ptr_temp,
1976 : ref_qp_index_temp,
1977 : area_in_pixel);
1978 :
1979 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
1980 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
1981 : // if(encode_context_ptr->hl_rate_control_historgram_queue_head_index == queue_entry_index_temp2)
1982 0 : if (queue_entry_index_head_temp == queue_entry_index_temp2)
1983 0 : picture_control_set_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
1984 :
1985 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
1986 0 : queue_entry_index_temp++;
1987 : }
1988 : }
1989 : }
1990 : #endif
1991 0 : picture_control_set_ptr->tables_updated = tables_updated;
1992 :
1993 : // Looping over the window to find the percentage of bit allocation in each layer
1994 0 : if ((sequence_control_set_ptr->intra_period_length != -1) &&
1995 0 : ((int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length) &&
1996 0 : ((int32_t)picture_control_set_ptr->frames_in_sw > sequence_control_set_ptr->intra_period_length)) {
1997 0 : if (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
1998 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
1999 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
2000 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
2001 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
2002 : (queue_entry_index_head_temp < 0) ?
2003 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
2004 : queue_entry_index_head_temp;
2005 :
2006 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
2007 :
2008 : // This is set to false, so the last frame would go inside the loop
2009 0 : end_of_sequence_flag = EB_FALSE;
2010 :
2011 0 : while (!end_of_sequence_flag &&
2012 0 : queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->intra_period_length) {
2013 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
2014 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
2015 :
2016 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
2017 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
2018 : else
2019 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][selected_ref_qp];
2020 :
2021 0 : ref_qp_index_temp = (uint32_t)CLIP3(
2022 : sequence_control_set_ptr->static_config.min_qp_allowed,
2023 : sequence_control_set_ptr->static_config.max_qp_allowed,
2024 : ref_qp_index_temp);
2025 :
2026 0 : picture_control_set_ptr->total_bits_per_gop += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
2027 0 : picture_control_set_ptr->bits_per_sw_per_layer[hl_rate_control_histogram_ptr_temp->temporal_layer_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
2028 0 : picture_control_set_ptr->percentage_updated = EB_TRUE;
2029 :
2030 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
2031 0 : queue_entry_index_temp++;
2032 : }
2033 0 : if (picture_control_set_ptr->total_bits_per_gop == 0) {
2034 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
2035 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_index];
2036 : }
2037 : }
2038 : }
2039 : else {
2040 0 : for (temporal_layer_index = 0; temporal_layer_index < EB_MAX_TEMPORAL_LAYERS; temporal_layer_index++)
2041 0 : picture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_index] = rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_index];
2042 : }
2043 :
2044 : // Set the QP
2045 0 : previous_selected_ref_qp = selected_ref_qp;
2046 0 : if (picture_control_set_ptr->picture_number > max_coded_poc && picture_control_set_ptr->temporal_layer_index < 2 && !picture_control_set_ptr->end_of_sequence_region) {
2047 0 : max_coded_poc = picture_control_set_ptr->picture_number;
2048 0 : max_coded_poc_selected_ref_qp = previous_selected_ref_qp;
2049 0 : encode_context_ptr->previous_selected_ref_qp = previous_selected_ref_qp;
2050 0 : encode_context_ptr->max_coded_poc = max_coded_poc;
2051 0 : encode_context_ptr->max_coded_poc_selected_ref_qp = max_coded_poc_selected_ref_qp;
2052 : }
2053 :
2054 0 : if (picture_control_set_ptr->slice_type == I_SLICE)
2055 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
2056 : else
2057 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][selected_ref_qp];
2058 :
2059 0 : picture_control_set_ptr->target_bits_best_pred_qp = picture_control_set_ptr->pred_bits_ref_qp[picture_control_set_ptr->best_pred_qp];
2060 0 : picture_control_set_ptr->best_pred_qp = (uint8_t)CLIP3(
2061 : sequence_control_set_ptr->static_config.min_qp_allowed,
2062 : sequence_control_set_ptr->static_config.max_qp_allowed,
2063 : (uint8_t)((int)picture_control_set_ptr->best_pred_qp + delta_qp));
2064 :
2065 : #if RC_UPDATE_TARGET_RATE
2066 0 : if (picture_control_set_ptr->picture_number == 0) {
2067 0 : high_level_rate_control_ptr->prev_intra_selected_ref_qp = selected_ref_qp;
2068 0 : high_level_rate_control_ptr->prev_intra_org_selected_ref_qp = selected_ref_qp;
2069 : }
2070 0 : if (sequence_control_set_ptr->intra_period_length != -1) {
2071 0 : if (picture_control_set_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
2072 0 : high_level_rate_control_ptr->prev_intra_selected_ref_qp = selected_ref_qp;
2073 0 : high_level_rate_control_ptr->prev_intra_org_selected_ref_qp = selected_org_ref_qp;
2074 : }
2075 : }
2076 : #endif
2077 : #if RC_PRINTS
2078 : ////if (picture_control_set_ptr->slice_type == 2)
2079 : {
2080 : SVT_LOG("\nTID: %d\t", picture_control_set_ptr->temporal_layer_index);
2081 : SVT_LOG("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",
2082 : picture_control_set_ptr->picture_number,
2083 : picture_control_set_ptr->best_pred_qp,
2084 : selected_ref_qp,
2085 : (int)picture_control_set_ptr->target_bits_best_pred_qp,
2086 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp - 1],
2087 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp],
2088 : (int)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[selected_ref_qp + 1],
2089 : (int)high_level_rate_control_ptr->bit_constraint_per_sw,
2090 : (int)bit_constraint_per_sw/*,
2091 : (int)high_level_rate_control_ptr->virtual_buffer_level*/);
2092 : }
2093 : #endif
2094 : }
2095 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->rate_table_update_mutex);
2096 0 : }
2097 0 : void frame_level_rc_input_picture_cvbr(
2098 : PictureControlSet *picture_control_set_ptr,
2099 : SequenceControlSet *sequence_control_set_ptr,
2100 : RateControlContext *context_ptr,
2101 : RateControlLayerContext *rate_control_layer_ptr,
2102 : RateControlIntervalParamContext *rate_control_param_ptr)
2103 : {
2104 : RateControlLayerContext *rate_control_layer_temp_ptr;
2105 :
2106 : // Tiles
2107 : uint32_t picture_area_in_pixel;
2108 : uint32_t area_in_pixel;
2109 :
2110 : // LCU Loop variables
2111 : SbParams *sb_params_ptr;
2112 : uint32_t sb_index;
2113 : uint64_t temp_qp;
2114 : uint32_t area_in_sbs;
2115 :
2116 0 : picture_area_in_pixel = sequence_control_set_ptr->seq_header.max_frame_height*sequence_control_set_ptr->seq_header.max_frame_width;
2117 :
2118 0 : if (rate_control_layer_ptr->first_frame == 1) {
2119 0 : rate_control_layer_ptr->first_frame = 0;
2120 0 : picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer = 1;
2121 : }
2122 : else
2123 0 : picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer = 0;
2124 0 : if (picture_control_set_ptr->slice_type != I_SLICE) {
2125 0 : if (rate_control_layer_ptr->first_non_intra_frame == 1) {
2126 0 : rate_control_layer_ptr->first_non_intra_frame = 0;
2127 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 1;
2128 : }
2129 : else
2130 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 0;
2131 : }
2132 : else
2133 0 : picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer = 0;
2134 :
2135 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = 0;
2136 :
2137 : // ***Rate Control***
2138 0 : area_in_sbs = 0;
2139 0 : area_in_pixel = 0;
2140 :
2141 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2142 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
2143 :
2144 0 : if (sb_params_ptr->is_complete_sb) {
2145 : // add the area of one LCU (64x64=4096) to the area of the tile
2146 0 : area_in_pixel += 4096;
2147 0 : area_in_sbs++;
2148 : }
2149 : else {
2150 : // add the area of the LCU to the area of the tile
2151 0 : area_in_pixel += sb_params_ptr->width * sb_params_ptr->height;
2152 : }
2153 : }
2154 0 : rate_control_layer_ptr->area_in_pixel = area_in_pixel;
2155 :
2156 0 : if (picture_control_set_ptr->parent_pcs_ptr->first_frame_in_temporal_layer || (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc)) {
2157 0 : if (sequence_control_set_ptr->static_config.enable_qp_scaling_flag && (picture_control_set_ptr->picture_number != rate_control_param_ptr->first_poc)) {
2158 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2159 : (int32_t)sequence_control_set_ptr->static_config.min_qp_allowed,
2160 : (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed,
2161 : (int32_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]));
2162 : }
2163 :
2164 0 : if (picture_control_set_ptr->picture_number == 0) {
2165 0 : rate_control_param_ptr->intra_frames_qp = sequence_control_set_ptr->qp;
2166 0 : rate_control_param_ptr->intra_frames_qp_bef_scal = (uint8_t)sequence_control_set_ptr->qp;
2167 : }
2168 :
2169 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
2170 : uint32_t temporal_layer_idex;
2171 0 : rate_control_param_ptr->previous_virtual_buffer_level = context_ptr->virtual_buffer_level_initial_value;
2172 0 : rate_control_param_ptr->virtual_buffer_level = context_ptr->virtual_buffer_level_initial_value;
2173 0 : rate_control_param_ptr->extra_ap_bit_ratio_i = 0;
2174 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
2175 0 : rate_control_param_ptr->last_poc = MAX(rate_control_param_ptr->first_poc + picture_control_set_ptr->parent_pcs_ptr->frames_in_sw - 1, rate_control_param_ptr->first_poc);
2176 0 : rate_control_param_ptr->last_gop = EB_TRUE;
2177 : }
2178 :
2179 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
2180 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
2181 0 : rate_control_layer_reset(
2182 : rate_control_layer_temp_ptr,
2183 : picture_control_set_ptr,
2184 : context_ptr,
2185 : picture_area_in_pixel,
2186 0 : rate_control_param_ptr->was_used);
2187 : }
2188 : }
2189 :
2190 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = 0;
2191 : // Finding the QP of the Intra frame by using variance tables
2192 0 : if (picture_control_set_ptr->slice_type == I_SLICE) {
2193 : uint32_t selected_ref_qp;
2194 :
2195 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance == 0)
2196 0 : printf("ERROR: LAD=0 is not supported\n");
2197 : else {
2198 0 : selected_ref_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
2199 0 : picture_control_set_ptr->picture_qp = (uint8_t)selected_ref_qp;
2200 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->picture_qp;
2201 : }
2202 :
2203 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2204 : sequence_control_set_ptr->static_config.min_qp_allowed,
2205 : sequence_control_set_ptr->static_config.max_qp_allowed,
2206 : picture_control_set_ptr->picture_qp);
2207 : }
2208 : else {
2209 : // LCU Loop
2210 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2211 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
2212 :
2213 0 : if (sb_params_ptr->is_complete_sb)
2214 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me += picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_index];
2215 : }
2216 :
2217 : // tileSadMe is normalized based on the area because of the LCUs at the tile boundries
2218 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me*rate_control_layer_ptr->area_in_pixel / (area_in_sbs << 12)), 1);
2219 :
2220 : // totalSquareMad has RC_PRECISION precision
2221 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me <<= RC_PRECISION;
2222 : }
2223 :
2224 0 : temp_qp = picture_control_set_ptr->picture_qp;
2225 :
2226 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
2227 : uint32_t temporal_layer_idex;
2228 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
2229 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
2230 0 : rate_control_layer_reset_part2(
2231 : context_ptr,
2232 : rate_control_layer_temp_ptr,
2233 : picture_control_set_ptr);
2234 : }
2235 : }
2236 :
2237 0 : if (picture_control_set_ptr->picture_number == 0) {
2238 0 : context_ptr->base_layer_frames_avg_qp = picture_control_set_ptr->picture_qp + 1;
2239 0 : context_ptr->base_layer_intra_frames_avg_qp = picture_control_set_ptr->picture_qp;
2240 : }
2241 : }
2242 : else {
2243 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = 0;
2244 :
2245 0 : HighLevelRateControlContext *high_level_rate_control_ptr = context_ptr->high_level_rate_control_ptr;
2246 0 : EncodeContext *encode_context_ptr = sequence_control_set_ptr->encode_context_ptr;
2247 : HlRateControlHistogramEntry *hl_rate_control_histogram_ptr_temp;
2248 : // Queue variables
2249 : uint32_t queue_entry_index_temp;
2250 : uint32_t queue_entry_index_temp2;
2251 : int64_t queue_entry_index_head_temp;
2252 :
2253 : uint64_t min_la_bit_distance;
2254 : uint32_t selected_ref_qp_table_index;
2255 : uint32_t selected_ref_qp;
2256 0 : uint32_t previous_selected_ref_qp = encode_context_ptr->previous_selected_ref_qp;
2257 :
2258 : uint32_t ref_qp_index;
2259 : uint32_t ref_qp_index_temp;
2260 : uint32_t ref_qp_table_index;
2261 :
2262 : uint32_t qp_search_min;
2263 : uint32_t qp_search_max;
2264 0 : int32_t qp_step = 1;
2265 : EbBool best_qp_found;
2266 :
2267 0 : uint64_t bit_constraint_per_sw = 0;
2268 0 : EbBool end_of_sequence_flag = EB_TRUE;
2269 :
2270 : // Loop over the QPs and find the best QP
2271 0 : min_la_bit_distance = MAX_UNSIGNED_VALUE;
2272 0 : qp_search_min = (uint8_t)CLIP3(
2273 : sequence_control_set_ptr->static_config.min_qp_allowed,
2274 : MAX_REF_QP_NUM,//sequence_control_set_ptr->static_config.max_qp_allowed,
2275 : (uint32_t)MAX((int32_t)sequence_control_set_ptr->qp - 40, 0));
2276 :
2277 0 : qp_search_max = (uint8_t)CLIP3(
2278 : sequence_control_set_ptr->static_config.min_qp_allowed,
2279 : MAX_REF_QP_NUM,
2280 : sequence_control_set_ptr->qp + 40);
2281 :
2282 0 : for (ref_qp_table_index = qp_search_min; ref_qp_table_index < qp_search_max; ref_qp_table_index++)
2283 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_table_index] = 0;
2284 : // Finding the predicted bits for each frame in the sliding window at the reference Qp(s)
2285 : ///queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
2286 0 : queue_entry_index_head_temp = (int32_t)(rate_control_param_ptr->first_poc - encode_context_ptr->hl_rate_control_historgram_queue[encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
2287 0 : queue_entry_index_head_temp += encode_context_ptr->hl_rate_control_historgram_queue_head_index;
2288 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
2289 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
2290 : (queue_entry_index_head_temp < 0) ?
2291 0 : queue_entry_index_head_temp + HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
2292 : queue_entry_index_head_temp;
2293 :
2294 0 : if (picture_control_set_ptr->parent_pcs_ptr->picture_number + picture_control_set_ptr->parent_pcs_ptr->frames_in_sw > rate_control_param_ptr->first_poc + sequence_control_set_ptr->static_config.intra_period_length + 1)
2295 0 : bit_constraint_per_sw = high_level_rate_control_ptr->bit_constraint_per_sw;
2296 : else
2297 0 : bit_constraint_per_sw = high_level_rate_control_ptr->bit_constraint_per_sw * encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_head_temp]->frames_in_sw / (sequence_control_set_ptr->static_config.look_ahead_distance + 1);
2298 :
2299 : // Loop over proper QPs and find the Predicted bits for that QP. Find the QP with the closest total predicted rate to target bits for the sliding window.
2300 0 : previous_selected_ref_qp = CLIP3(
2301 : qp_search_min + 1,
2302 : qp_search_max - 1,
2303 : previous_selected_ref_qp);
2304 0 : ref_qp_table_index = previous_selected_ref_qp;
2305 0 : ref_qp_index = ref_qp_table_index;
2306 0 : selected_ref_qp_table_index = ref_qp_table_index;
2307 0 : selected_ref_qp = selected_ref_qp_table_index;
2308 0 : best_qp_found = EB_FALSE;
2309 0 : while (ref_qp_table_index >= qp_search_min && ref_qp_table_index <= qp_search_max && !best_qp_found) {
2310 0 : ref_qp_index = CLIP3(
2311 : sequence_control_set_ptr->static_config.min_qp_allowed,
2312 : MAX_REF_QP_NUM,
2313 : ref_qp_table_index);
2314 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] = 0;
2315 :
2316 0 : queue_entry_index_temp = (uint32_t) queue_entry_index_head_temp;
2317 : // This is set to false, so the last frame would go inside the loop
2318 0 : end_of_sequence_flag = EB_FALSE;
2319 :
2320 0 : while (!end_of_sequence_flag &&
2321 : //queue_entry_index_temp <= queue_entry_index_head_temp + sequence_control_set_ptr->static_config.look_ahead_distance) {
2322 0 : queue_entry_index_temp <= queue_entry_index_head_temp + encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_head_temp]->frames_in_sw - 1) {
2323 0 : queue_entry_index_temp2 = (queue_entry_index_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH : queue_entry_index_temp;
2324 0 : hl_rate_control_histogram_ptr_temp = (encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_temp2]);
2325 :
2326 0 : if (hl_rate_control_histogram_ptr_temp->slice_type == I_SLICE)
2327 0 : ref_qp_index_temp = context_ptr->qp_scaling_map_I_SLICE[ref_qp_index];
2328 : else
2329 0 : ref_qp_index_temp = context_ptr->qp_scaling_map[hl_rate_control_histogram_ptr_temp->temporal_layer_index][ref_qp_index];
2330 :
2331 0 : ref_qp_index_temp = (uint32_t)CLIP3(
2332 : sequence_control_set_ptr->static_config.min_qp_allowed,
2333 : sequence_control_set_ptr->static_config.max_qp_allowed,
2334 : ref_qp_index_temp);
2335 :
2336 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = 0;
2337 :
2338 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
2339 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
2340 0 : hl_rate_control_histogram_ptr_temp->life_count--;
2341 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
2342 : }
2343 :
2344 0 : hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp] = predict_bits(
2345 : encode_context_ptr,
2346 : hl_rate_control_histogram_ptr_temp,
2347 : ref_qp_index_temp,
2348 : area_in_pixel);
2349 :
2350 0 : high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] += hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
2351 : // Store the pred_bits_ref_qp for the first frame in the window to PCS
2352 0 : if (queue_entry_index_head_temp == queue_entry_index_temp2)
2353 0 : picture_control_set_ptr->parent_pcs_ptr->pred_bits_ref_qp[ref_qp_index_temp] = hl_rate_control_histogram_ptr_temp->pred_bits_ref_qp[ref_qp_index_temp];
2354 :
2355 0 : end_of_sequence_flag = hl_rate_control_histogram_ptr_temp->end_of_sequence_flag;
2356 0 : queue_entry_index_temp++;
2357 : }
2358 :
2359 0 : if (min_la_bit_distance >= (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw)) {
2360 0 : min_la_bit_distance = (uint64_t)ABS((int64_t)high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - (int64_t)bit_constraint_per_sw);
2361 0 : selected_ref_qp_table_index = ref_qp_table_index;
2362 0 : selected_ref_qp = ref_qp_index;
2363 : }
2364 : else
2365 0 : best_qp_found = EB_TRUE;
2366 0 : if (ref_qp_table_index == previous_selected_ref_qp) {
2367 0 : if (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] > bit_constraint_per_sw)
2368 0 : qp_step = +1;
2369 : else
2370 0 : qp_step = -1;
2371 : }
2372 0 : ref_qp_table_index = (uint32_t)(ref_qp_table_index + qp_step);
2373 : }
2374 :
2375 0 : int delta_qp = 0;
2376 0 : if (ref_qp_index == sequence_control_set_ptr->static_config.max_qp_allowed && high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] > bit_constraint_per_sw) {
2377 0 : delta_qp = (int)((high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index] - bit_constraint_per_sw) * 100 / (high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index - 1] - high_level_rate_control_ptr->pred_bits_ref_qpPerSw[ref_qp_index]));
2378 0 : delta_qp = (delta_qp + 50) / 100;
2379 : }
2380 :
2381 0 : if (picture_control_set_ptr->slice_type == I_SLICE)
2382 0 : picture_control_set_ptr->parent_pcs_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map_I_SLICE[selected_ref_qp];
2383 : else
2384 0 : picture_control_set_ptr->parent_pcs_ptr->best_pred_qp = (uint8_t)context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][selected_ref_qp];
2385 :
2386 0 : picture_control_set_ptr->parent_pcs_ptr->best_pred_qp = (uint8_t)CLIP3(
2387 : sequence_control_set_ptr->static_config.min_qp_allowed,
2388 : sequence_control_set_ptr->static_config.max_qp_allowed,
2389 : (uint8_t)((int)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp + delta_qp));
2390 :
2391 : // if the pixture is an I slice, for now we set the QP as the QP of the previous frame
2392 0 : if (picture_control_set_ptr->slice_type == I_SLICE) {
2393 : uint32_t selected_ref_qp;
2394 :
2395 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance == 0)
2396 0 : printf("ERROR: LAD=0 is not supported\n");
2397 : else {
2398 0 : selected_ref_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
2399 0 : picture_control_set_ptr->picture_qp = (uint8_t)selected_ref_qp;
2400 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->picture_qp;
2401 : }
2402 :
2403 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2404 : sequence_control_set_ptr->static_config.min_qp_allowed,
2405 : sequence_control_set_ptr->static_config.max_qp_allowed,
2406 : picture_control_set_ptr->picture_qp);
2407 :
2408 0 : temp_qp = picture_control_set_ptr->picture_qp;
2409 : }
2410 :
2411 : else { // Not an I slice
2412 : // combining the target rate from initial RC and frame level RC
2413 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
2414 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = rate_control_layer_ptr->bit_constraint;
2415 0 : rate_control_layer_ptr->ec_bit_constraint = (rate_control_layer_ptr->alpha * picture_control_set_ptr->parent_pcs_ptr->target_bits_best_pred_qp +
2416 0 : ((1 << RC_PRECISION) - rate_control_layer_ptr->alpha) * picture_control_set_ptr->parent_pcs_ptr->target_bits_rc + RC_PRECISION_OFFSET) >> RC_PRECISION;
2417 :
2418 0 : rate_control_layer_ptr->ec_bit_constraint = (uint64_t)MAX((int64_t)rate_control_layer_ptr->ec_bit_constraint - (int64_t)rate_control_layer_ptr->dif_total_and_ec_bits, 1);
2419 :
2420 0 : picture_control_set_ptr->parent_pcs_ptr->target_bits_rc = rate_control_layer_ptr->ec_bit_constraint;
2421 : }
2422 :
2423 : // LCU Loop
2424 0 : for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2425 0 : sb_params_ptr = &sequence_control_set_ptr->sb_params_array[sb_index];
2426 :
2427 0 : if (sb_params_ptr->is_complete_sb)
2428 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me += picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_index];
2429 : }
2430 :
2431 : // tileSadMe is normalized based on the area because of the LCUs at the tile boundries
2432 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me*rate_control_layer_ptr->area_in_pixel / (area_in_sbs << 12)), 1);
2433 0 : picture_control_set_ptr->parent_pcs_ptr->sad_me <<= RC_PRECISION;
2434 0 : if (rate_control_layer_ptr->area_in_pixel > 0)
2435 0 : rate_control_layer_ptr->total_mad = MAX((picture_control_set_ptr->parent_pcs_ptr->sad_me / rate_control_layer_ptr->area_in_pixel), 1);
2436 0 : if (!rate_control_layer_ptr->feedback_arrived)
2437 0 : rate_control_layer_ptr->previous_frame_distortion_me = picture_control_set_ptr->parent_pcs_ptr->sad_me;
2438 : {
2439 : uint64_t qp_calc_temp1, qp_calc_temp2, qp_calc_temp3;
2440 :
2441 0 : qp_calc_temp1 = picture_control_set_ptr->parent_pcs_ptr->sad_me *rate_control_layer_ptr->total_mad;
2442 0 : qp_calc_temp2 =
2443 0 : MAX((int64_t)(rate_control_layer_ptr->ec_bit_constraint << (2 * RC_PRECISION)) - (int64_t)rate_control_layer_ptr->c_coeff*(int64_t)rate_control_layer_ptr->area_in_pixel,
2444 : (int64_t)(rate_control_layer_ptr->ec_bit_constraint << (2 * RC_PRECISION - 2)));
2445 :
2446 : // This is a more complex but with higher precision implementation
2447 0 : if (qp_calc_temp1 > qp_calc_temp2)
2448 0 : qp_calc_temp3 = (uint64_t)((qp_calc_temp1 / qp_calc_temp2)*rate_control_layer_ptr->k_coeff);
2449 : else
2450 0 : qp_calc_temp3 = (uint64_t)(qp_calc_temp1*rate_control_layer_ptr->k_coeff / qp_calc_temp2);
2451 0 : temp_qp = (uint64_t)(log2f_high_precision(MAX(((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION)*((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION)*((qp_calc_temp3 + RC_PRECISION_OFFSET) >> RC_PRECISION), 1), RC_PRECISION));
2452 :
2453 0 : rate_control_layer_ptr->calculated_frame_qp = (uint8_t)(CLIP3(1, 63, (uint32_t)(temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION));
2454 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = (uint8_t)(CLIP3(1, 63, (uint32_t)(temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION));
2455 : }
2456 :
2457 0 : temp_qp += rate_control_layer_ptr->delta_qp_fraction;
2458 0 : picture_control_set_ptr->picture_qp = (uint8_t)((temp_qp + RC_PRECISION_OFFSET) >> RC_PRECISION);
2459 : // Use the QP of HLRC instead of calculated one in FLRC
2460 0 : if (picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels > 1) {
2461 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
2462 0 : picture_control_set_ptr->parent_pcs_ptr->calculated_qp = picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
2463 : }
2464 : }
2465 0 : if (picture_control_set_ptr->parent_pcs_ptr->first_non_intra_frame_in_temporal_layer && picture_control_set_ptr->temporal_layer_index == 0 && picture_control_set_ptr->slice_type != I_SLICE)
2466 0 : picture_control_set_ptr->picture_qp = (uint8_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]);
2467 0 : if (!rate_control_layer_ptr->feedback_arrived && picture_control_set_ptr->slice_type != I_SLICE) {
2468 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2469 : (int32_t)sequence_control_set_ptr->static_config.min_qp_allowed,
2470 : (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed,
2471 : (int32_t)(rate_control_param_ptr->intra_frames_qp + context_ptr->qp_scaling_map[picture_control_set_ptr->temporal_layer_index][rate_control_param_ptr->intra_frames_qp_bef_scal] - context_ptr->qp_scaling_map_I_SLICE[rate_control_param_ptr->intra_frames_qp_bef_scal]));
2472 : }
2473 :
2474 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
2475 0 : if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 2)
2476 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 4;
2477 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 1)
2478 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 3;
2479 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2)
2480 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 2;
2481 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold1 &&
2482 0 : rate_control_param_ptr->virtual_buffer_level < context_ptr->vb_fill_threshold2) {
2483 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD1QPINCREASE + 2;
2484 : }
2485 : }
2486 : else {
2487 : //if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 2){
2488 0 : if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 + (int64_t)(context_ptr->virtual_buffer_size * 2 / 3))
2489 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 2;
2490 : //else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 << 1){
2491 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2 + (int64_t)(context_ptr->virtual_buffer_size / 3))
2492 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 1;
2493 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold2)
2494 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD2QPINCREASE + 1;
2495 0 : else if (rate_control_param_ptr->virtual_buffer_level > context_ptr->vb_fill_threshold1 &&
2496 0 : rate_control_param_ptr->virtual_buffer_level < context_ptr->vb_fill_threshold2) {
2497 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + (uint8_t)THRESHOLD1QPINCREASE;
2498 : }
2499 : }
2500 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
2501 0 : if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 2))
2502 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 2, 0);
2503 0 : else if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 1))
2504 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 1, 0);
2505 0 : else if (rate_control_param_ptr->virtual_buffer_level < 0)
2506 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
2507 : }
2508 : else {
2509 0 : if (rate_control_param_ptr->virtual_buffer_level < -(context_ptr->vb_fill_threshold2 << 2))
2510 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE - 1, 0);
2511 0 : else if (rate_control_param_ptr->virtual_buffer_level < -context_ptr->vb_fill_threshold2)
2512 0 : picture_control_set_ptr->picture_qp = (uint8_t)MAX((int32_t)picture_control_set_ptr->picture_qp - (int32_t)THRESHOLD2QPINCREASE, 0);
2513 : }
2514 :
2515 : uint32_t ref_qp;
2516 0 : if ((int32_t)picture_control_set_ptr->temporal_layer_index == 0 && picture_control_set_ptr->slice_type != I_SLICE) {
2517 0 : if (picture_control_set_ptr->ref_slice_type_array[0][0] == I_SLICE) {
2518 : /* picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2519 : (uint32_t)picture_control_set_ptr->ref_pic_qp_array[0],
2520 : (uint32_t)picture_control_set_ptr->picture_qp,
2521 : picture_control_set_ptr->picture_qp);*/
2522 : }
2523 : else {
2524 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2525 : (uint32_t)MAX((int32_t)picture_control_set_ptr->ref_pic_qp_array[0][0] - 1, 0),
2526 : (uint32_t)picture_control_set_ptr->ref_pic_qp_array[0][0] + 3,
2527 : picture_control_set_ptr->picture_qp);
2528 : }
2529 : }
2530 : else {
2531 0 : ref_qp = 0;
2532 0 : if (picture_control_set_ptr->ref_slice_type_array[0][0] != I_SLICE)
2533 0 : ref_qp = MAX(ref_qp, picture_control_set_ptr->ref_pic_qp_array[0][0]);
2534 0 : if ((picture_control_set_ptr->slice_type == B_SLICE) && (picture_control_set_ptr->ref_slice_type_array[1][0] != I_SLICE))
2535 0 : ref_qp = MAX(ref_qp, picture_control_set_ptr->ref_pic_qp_array[1][0]);
2536 0 : if (ref_qp > 0) {
2537 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2538 : (uint32_t)ref_qp - 1,
2539 : picture_control_set_ptr->picture_qp,
2540 : picture_control_set_ptr->picture_qp);
2541 : }
2542 : }
2543 : // limiting the QP between min Qp allowed and max Qp allowed
2544 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2545 : sequence_control_set_ptr->static_config.min_qp_allowed,
2546 : sequence_control_set_ptr->static_config.max_qp_allowed,
2547 : picture_control_set_ptr->picture_qp);
2548 :
2549 0 : rate_control_layer_ptr->delta_qp_fraction = CLIP3(-RC_PRECISION_OFFSET, RC_PRECISION_OFFSET, -((int64_t)temp_qp - (int64_t)(picture_control_set_ptr->picture_qp << RC_PRECISION)));
2550 :
2551 0 : if (picture_control_set_ptr->parent_pcs_ptr->sad_me == rate_control_layer_ptr->previous_frame_distortion_me &&
2552 0 : (rate_control_layer_ptr->previous_frame_distortion_me != 0))
2553 0 : rate_control_layer_ptr->same_distortion_count++;
2554 : else
2555 0 : rate_control_layer_ptr->same_distortion_count = 0;
2556 : }
2557 :
2558 0 : rate_control_layer_ptr->previous_c_coeff = rate_control_layer_ptr->c_coeff;
2559 0 : rate_control_layer_ptr->previous_k_coeff = rate_control_layer_ptr->k_coeff;
2560 0 : rate_control_layer_ptr->previous_calculated_frame_qp = rate_control_layer_ptr->calculated_frame_qp;
2561 0 : }
2562 :
2563 0 : void frame_level_rc_feedback_picture_cvbr(
2564 : PictureParentControlSet *parentpicture_control_set_ptr,
2565 : SequenceControlSet *sequence_control_set_ptr,
2566 : RateControlContext *context_ptr)
2567 : {
2568 : RateControlLayerContext *rate_control_layer_temp_ptr;
2569 : RateControlIntervalParamContext *rate_control_param_ptr;
2570 : RateControlLayerContext *rate_control_layer_ptr;
2571 : // LCU Loop variables
2572 : uint32_t slice_num;
2573 : uint64_t previous_frame_bit_actual;
2574 :
2575 0 : if (sequence_control_set_ptr->intra_period_length == -1)
2576 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
2577 : else {
2578 0 : uint32_t interval_index_temp = 0;
2579 0 : while ((!(parentpicture_control_set_ptr->picture_number >= context_ptr->rate_control_param_queue[interval_index_temp]->first_poc &&
2580 0 : parentpicture_control_set_ptr->picture_number <= context_ptr->rate_control_param_queue[interval_index_temp]->last_poc)) &&
2581 : (interval_index_temp < PARALLEL_GOP_MAX_NUMBER)) {
2582 0 : interval_index_temp++;
2583 : }
2584 0 : CHECK_REPORT_ERROR(
2585 : interval_index_temp != PARALLEL_GOP_MAX_NUMBER,
2586 : sequence_control_set_ptr->encode_context_ptr->app_callback_ptr,
2587 : EB_ENC_RC_ERROR2);
2588 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[interval_index_temp];
2589 : }
2590 :
2591 0 : rate_control_layer_ptr = rate_control_param_ptr->rate_control_layer_array[parentpicture_control_set_ptr->temporal_layer_index];
2592 :
2593 0 : rate_control_layer_ptr->max_qp = 0;
2594 :
2595 0 : rate_control_layer_ptr->feedback_arrived = EB_TRUE;
2596 0 : rate_control_layer_ptr->max_qp = MAX(rate_control_layer_ptr->max_qp, parentpicture_control_set_ptr->picture_qp);
2597 :
2598 0 : rate_control_layer_ptr->previous_frame_qp = parentpicture_control_set_ptr->picture_qp;
2599 0 : rate_control_layer_ptr->previous_frame_bit_actual = parentpicture_control_set_ptr->total_num_bits;
2600 0 : if (parentpicture_control_set_ptr->quantized_coeff_num_bits == 0)
2601 0 : parentpicture_control_set_ptr->quantized_coeff_num_bits = 1;
2602 0 : rate_control_layer_ptr->previous_framequantized_coeff_bit_actual = parentpicture_control_set_ptr->quantized_coeff_num_bits;
2603 :
2604 : // Setting Critical states for adjusting the averaging weights on C and K
2605 0 : if ((parentpicture_control_set_ptr->sad_me > (3 * rate_control_layer_ptr->previous_frame_distortion_me) >> 1) &&
2606 0 : (rate_control_layer_ptr->previous_frame_distortion_me != 0)) {
2607 0 : rate_control_layer_ptr->critical_states = 3;
2608 : }
2609 0 : else if (rate_control_layer_ptr->critical_states)
2610 0 : rate_control_layer_ptr->critical_states--;
2611 : else
2612 0 : rate_control_layer_ptr->critical_states = 0;
2613 0 : if (parentpicture_control_set_ptr->slice_type != I_SLICE) {
2614 : // Updating c_coeff
2615 0 : rate_control_layer_ptr->c_coeff = (((int64_t)rate_control_layer_ptr->previous_frame_bit_actual - (int64_t)rate_control_layer_ptr->previous_framequantized_coeff_bit_actual) << (2 * RC_PRECISION))
2616 0 : / rate_control_layer_ptr->area_in_pixel;
2617 0 : rate_control_layer_ptr->c_coeff = MAX(rate_control_layer_ptr->c_coeff, 1);
2618 :
2619 : // Updating k_coeff
2620 0 : if ((parentpicture_control_set_ptr->sad_me + RC_PRECISION_OFFSET) >> RC_PRECISION > 5) {
2621 : {
2622 : uint64_t test1, test2, test3;
2623 0 : test1 = rate_control_layer_ptr->previous_framequantized_coeff_bit_actual*(two_to_power_qp_over_three[parentpicture_control_set_ptr->picture_qp]);
2624 0 : test2 = MAX(parentpicture_control_set_ptr->sad_me / rate_control_layer_ptr->area_in_pixel, 1);
2625 0 : test3 = test1 * 65536 / test2 * 65536 / parentpicture_control_set_ptr->sad_me;
2626 :
2627 0 : rate_control_layer_ptr->k_coeff = test3;
2628 : }
2629 : }
2630 :
2631 0 : if (rate_control_layer_ptr->critical_states) {
2632 0 : rate_control_layer_ptr->k_coeff = (8 * rate_control_layer_ptr->k_coeff + 8 * rate_control_layer_ptr->previous_k_coeff + 8) >> 4;
2633 0 : rate_control_layer_ptr->c_coeff = (8 * rate_control_layer_ptr->c_coeff + 8 * rate_control_layer_ptr->previous_c_coeff + 8) >> 4;
2634 : }
2635 : else {
2636 0 : rate_control_layer_ptr->k_coeff = (rate_control_layer_ptr->coeff_averaging_weight1*rate_control_layer_ptr->k_coeff + rate_control_layer_ptr->coeff_averaging_weight2*rate_control_layer_ptr->previous_k_coeff + 8) >> 4;
2637 0 : rate_control_layer_ptr->c_coeff = (rate_control_layer_ptr->coeff_averaging_weight1*rate_control_layer_ptr->c_coeff + rate_control_layer_ptr->coeff_averaging_weight2*rate_control_layer_ptr->previous_c_coeff + 8) >> 4;
2638 : }
2639 0 : rate_control_layer_ptr->k_coeff = MIN(rate_control_layer_ptr->k_coeff, rate_control_layer_ptr->previous_k_coeff * 4);
2640 0 : rate_control_layer_ptr->c_coeff = MIN(rate_control_layer_ptr->c_coeff, rate_control_layer_ptr->previous_c_coeff * 4);
2641 0 : if (parentpicture_control_set_ptr->slice_type != I_SLICE)
2642 0 : rate_control_layer_ptr->previous_frame_distortion_me = parentpicture_control_set_ptr->sad_me;
2643 : else
2644 0 : rate_control_layer_ptr->previous_frame_distortion_me = 0;
2645 : }
2646 :
2647 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
2648 0 : if (parentpicture_control_set_ptr->slice_type == I_SLICE) {
2649 0 : if (parentpicture_control_set_ptr->total_num_bits < parentpicture_control_set_ptr->target_bits_best_pred_qp << 1)
2650 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 2) >> 2;
2651 0 : else if (parentpicture_control_set_ptr->total_num_bits > parentpicture_control_set_ptr->target_bits_best_pred_qp << 2)
2652 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 4 + 2) >> 2;
2653 0 : else if (parentpicture_control_set_ptr->total_num_bits > parentpicture_control_set_ptr->target_bits_best_pred_qp << 1)
2654 0 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + parentpicture_control_set_ptr->picture_qp + 2 + 2) >> 2;
2655 : }
2656 : }
2657 :
2658 : {
2659 0 : uint64_t previous_frame_ec_bits = 0;
2660 0 : EbBool picture_min_qp_allowed = EB_TRUE;
2661 0 : rate_control_layer_ptr->previous_frame_average_qp = 0;
2662 0 : rate_control_layer_ptr->previous_frame_average_qp += rate_control_layer_ptr->previous_frame_qp;
2663 0 : previous_frame_ec_bits += rate_control_layer_ptr->previous_frame_bit_actual;
2664 0 : if (rate_control_layer_ptr->same_distortion_count == 0 ||
2665 0 : parentpicture_control_set_ptr->picture_qp != sequence_control_set_ptr->static_config.min_qp_allowed) {
2666 0 : picture_min_qp_allowed = EB_FALSE;
2667 : }
2668 0 : if (picture_min_qp_allowed)
2669 0 : rate_control_layer_ptr->frame_same_distortion_min_qp_count++;
2670 : else
2671 0 : rate_control_layer_ptr->frame_same_distortion_min_qp_count = 0;
2672 :
2673 0 : rate_control_layer_ptr->previous_ec_bits = previous_frame_ec_bits;
2674 0 : previous_frame_bit_actual = parentpicture_control_set_ptr->total_num_bits;
2675 0 : if (parentpicture_control_set_ptr->first_frame_in_temporal_layer)
2676 0 : rate_control_layer_ptr->dif_total_and_ec_bits = (previous_frame_bit_actual - previous_frame_ec_bits);
2677 : else
2678 0 : rate_control_layer_ptr->dif_total_and_ec_bits = ((previous_frame_bit_actual - previous_frame_ec_bits) + rate_control_layer_ptr->dif_total_and_ec_bits) >> 1;
2679 : // update bitrate of different layers in the interval based on the rate of the I frame
2680 0 : if (parentpicture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc &&
2681 0 : (parentpicture_control_set_ptr->slice_type == I_SLICE) &&
2682 0 : sequence_control_set_ptr->static_config.intra_period_length != -1) {
2683 : uint32_t temporal_layer_idex;
2684 : uint64_t target_bit_rate;
2685 : uint64_t channel_bit_rate;
2686 0 : uint64_t sum_bits_per_sw = 0;
2687 : #if ADAPTIVE_PERCENTAGE
2688 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
2689 0 : if (parentpicture_control_set_ptr->tables_updated && parentpicture_control_set_ptr->percentage_updated) {
2690 0 : parentpicture_control_set_ptr->bits_per_sw_per_layer[0] =
2691 0 : (uint64_t)MAX((int64_t)parentpicture_control_set_ptr->bits_per_sw_per_layer[0] + (int64_t)parentpicture_control_set_ptr->total_num_bits - (int64_t)parentpicture_control_set_ptr->target_bits_best_pred_qp, 1);
2692 : }
2693 : }
2694 : #endif
2695 :
2696 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0 && sequence_control_set_ptr->intra_period_length != -1) {
2697 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++)
2698 0 : sum_bits_per_sw += parentpicture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_idex];
2699 : }
2700 :
2701 0 : for (temporal_layer_idex = 0; temporal_layer_idex < EB_MAX_TEMPORAL_LAYERS; temporal_layer_idex++) {
2702 0 : rate_control_layer_temp_ptr = rate_control_param_ptr->rate_control_layer_array[temporal_layer_idex];
2703 :
2704 0 : target_bit_rate = (uint64_t)((int64_t)parentpicture_control_set_ptr->target_bit_rate -
2705 0 : MIN((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4, (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION))
2706 0 : *rate_percentage_layer_array[sequence_control_set_ptr->static_config.hierarchical_levels][temporal_layer_idex] / 100;
2707 :
2708 : #if ADAPTIVE_PERCENTAGE
2709 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0 && sequence_control_set_ptr->intra_period_length != -1) {
2710 0 : target_bit_rate = (uint64_t)((int64_t)parentpicture_control_set_ptr->target_bit_rate -
2711 0 : MIN((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4, (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION))
2712 0 : *parentpicture_control_set_ptr->bits_per_sw_per_layer[temporal_layer_idex] / sum_bits_per_sw;
2713 : }
2714 : #endif
2715 : // update this based on temporal layers
2716 0 : if (temporal_layer_idex == 0)
2717 0 : channel_bit_rate = (((target_bit_rate << (2 * RC_PRECISION)) / MAX(1, rate_control_layer_temp_ptr->frame_rate - (1 * context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)))) + RC_PRECISION_OFFSET) >> RC_PRECISION;
2718 : else
2719 0 : channel_bit_rate = (((target_bit_rate << (2 * RC_PRECISION)) / rate_control_layer_temp_ptr->frame_rate) + RC_PRECISION_OFFSET) >> RC_PRECISION;
2720 0 : channel_bit_rate = (uint64_t)MAX((int64_t)1, (int64_t)channel_bit_rate);
2721 0 : rate_control_layer_temp_ptr->ec_bit_constraint = channel_bit_rate;
2722 :
2723 0 : slice_num = 1;
2724 0 : rate_control_layer_temp_ptr->ec_bit_constraint -= SLICE_HEADER_BITS_NUM * slice_num;
2725 :
2726 0 : rate_control_layer_temp_ptr->previous_bit_constraint = channel_bit_rate;
2727 0 : rate_control_layer_temp_ptr->bit_constraint = channel_bit_rate;
2728 0 : rate_control_layer_temp_ptr->channel_bit_rate = channel_bit_rate;
2729 : }
2730 0 : if ((int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4 < (int64_t)(parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION) {
2731 0 : rate_control_param_ptr->previous_virtual_buffer_level += (int64_t)((parentpicture_control_set_ptr->total_num_bits*context_ptr->frame_rate / (sequence_control_set_ptr->static_config.intra_period_length + 1)) >> RC_PRECISION) - (int64_t)parentpicture_control_set_ptr->target_bit_rate * 3 / 4;
2732 0 : context_ptr->extra_bits_gen = 0;
2733 : }
2734 : }
2735 :
2736 0 : if (previous_frame_bit_actual) {
2737 : uint64_t bit_changes_rate;
2738 : // Updating virtual buffer level and it can be negative
2739 0 : context_ptr->extra_bits_gen = 0;
2740 0 : rate_control_param_ptr->virtual_buffer_level =
2741 0 : (int64_t)rate_control_param_ptr->previous_virtual_buffer_level +
2742 0 : (int64_t)previous_frame_bit_actual - (int64_t)context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_frame;
2743 0 : if (parentpicture_control_set_ptr->hierarchical_levels > 1 && rate_control_layer_ptr->frame_same_distortion_min_qp_count > 10) {
2744 0 : rate_control_layer_ptr->previous_bit_constraint = (int64_t)rate_control_layer_ptr->channel_bit_rate;
2745 0 : rate_control_param_ptr->virtual_buffer_level = ((int64_t)context_ptr->virtual_buffer_size >> 1);
2746 : }
2747 : // Updating bit difference
2748 0 : rate_control_layer_ptr->bit_diff = (int64_t)rate_control_param_ptr->virtual_buffer_level
2749 : //- ((int64_t)context_ptr->virtual_buffer_size>>1);
2750 0 : - ((int64_t)rate_control_layer_ptr->channel_bit_rate >> 1);
2751 :
2752 : // Limit the bit difference
2753 0 : rate_control_layer_ptr->bit_diff = CLIP3(-(int64_t)(rate_control_layer_ptr->channel_bit_rate), (int64_t)(rate_control_layer_ptr->channel_bit_rate >> 1), rate_control_layer_ptr->bit_diff);
2754 0 : bit_changes_rate = rate_control_layer_ptr->frame_rate;
2755 :
2756 : // Updating bit Constraint
2757 0 : rate_control_layer_ptr->bit_constraint = MAX((int64_t)rate_control_layer_ptr->previous_bit_constraint - ((rate_control_layer_ptr->bit_diff << RC_PRECISION) / ((int64_t)bit_changes_rate)), 1);
2758 :
2759 : // Limiting the bit_constraint
2760 0 : if (parentpicture_control_set_ptr->temporal_layer_index == 0) {
2761 0 : rate_control_layer_ptr->bit_constraint = CLIP3(rate_control_layer_ptr->channel_bit_rate >> 2,
2762 : rate_control_layer_ptr->channel_bit_rate * 200 / 100,
2763 : rate_control_layer_ptr->bit_constraint);
2764 : }
2765 : else {
2766 0 : rate_control_layer_ptr->bit_constraint = CLIP3(rate_control_layer_ptr->channel_bit_rate >> 1,
2767 : rate_control_layer_ptr->channel_bit_rate * 200 / 100,
2768 : rate_control_layer_ptr->bit_constraint);
2769 : }
2770 0 : rate_control_layer_ptr->ec_bit_constraint = (uint64_t)MAX((int64_t)rate_control_layer_ptr->bit_constraint - (int64_t)rate_control_layer_ptr->dif_total_and_ec_bits, 1);
2771 0 : rate_control_param_ptr->previous_virtual_buffer_level = rate_control_param_ptr->virtual_buffer_level;
2772 0 : rate_control_layer_ptr->previous_bit_constraint = rate_control_layer_ptr->bit_constraint;
2773 : }
2774 :
2775 0 : rate_control_param_ptr->processed_frames_number++;
2776 0 : rate_control_param_ptr->in_use = EB_TRUE;
2777 : // check if all the frames in the interval have arrived
2778 0 : if (rate_control_param_ptr->processed_frames_number == (rate_control_param_ptr->last_poc - rate_control_param_ptr->first_poc + 1) &&
2779 0 : sequence_control_set_ptr->intra_period_length != -1) {
2780 : uint32_t temporal_index;
2781 : int64_t extra_bits;
2782 0 : rate_control_param_ptr->first_poc += PARALLEL_GOP_MAX_NUMBER * (uint32_t)(sequence_control_set_ptr->intra_period_length + 1);
2783 0 : rate_control_param_ptr->last_poc += PARALLEL_GOP_MAX_NUMBER * (uint32_t)(sequence_control_set_ptr->intra_period_length + 1);
2784 0 : rate_control_param_ptr->processed_frames_number = 0;
2785 0 : rate_control_param_ptr->extra_ap_bit_ratio_i = 0;
2786 0 : rate_control_param_ptr->in_use = EB_FALSE;
2787 0 : rate_control_param_ptr->was_used = EB_TRUE;
2788 0 : rate_control_param_ptr->last_gop = EB_FALSE;
2789 0 : rate_control_param_ptr->first_pic_actual_qp_assigned = EB_FALSE;
2790 0 : for (temporal_index = 0; temporal_index < EB_MAX_TEMPORAL_LAYERS; temporal_index++) {
2791 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->first_frame = 1;
2792 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->first_non_intra_frame = 1;
2793 0 : rate_control_param_ptr->rate_control_layer_array[temporal_index]->feedback_arrived = EB_FALSE;
2794 : }
2795 0 : extra_bits = ((int64_t)context_ptr->virtual_buffer_size >> 1) - (int64_t)rate_control_param_ptr->virtual_buffer_level;
2796 :
2797 0 : rate_control_param_ptr->virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
2798 0 : context_ptr->extra_bits += extra_bits;
2799 : }
2800 0 : context_ptr->extra_bits = 0;
2801 : }
2802 :
2803 : #if RC_PRINTS
2804 : ///if (parentpicture_control_set_ptr->temporal_layer_index == 0)
2805 : {
2806 : SVT_LOG("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%.0f\t%.0f\t%.0f\t%.0f\t%d\t%d\n",
2807 : (int)parentpicture_control_set_ptr->slice_type,
2808 : (int)parentpicture_control_set_ptr->picture_number,
2809 : (int)parentpicture_control_set_ptr->temporal_layer_index,
2810 : (int)parentpicture_control_set_ptr->picture_qp, (int)parentpicture_control_set_ptr->calculated_qp, (int)parentpicture_control_set_ptr->best_pred_qp,
2811 : (int)previous_frame_bit_actual,
2812 : (int)parentpicture_control_set_ptr->target_bits_best_pred_qp,
2813 : (int)parentpicture_control_set_ptr->target_bits_rc,
2814 : (int)rate_control_layer_ptr->channel_bit_rate,
2815 : (int)rate_control_layer_ptr->bit_constraint,
2816 : (double)rate_control_layer_ptr->c_coeff,
2817 : (double)rate_control_layer_ptr->k_coeff,
2818 : (double)parentpicture_control_set_ptr->sad_me,
2819 : (double)context_ptr->extra_bits_gen,
2820 : (int)rate_control_param_ptr->virtual_buffer_level,
2821 : (int)context_ptr->extra_bits);
2822 : }
2823 : #endif
2824 0 : }
2825 :
2826 0 : void high_level_rc_feed_back_picture(
2827 : PictureParentControlSet *picture_control_set_ptr,
2828 : SequenceControlSet *sequence_control_set_ptr)
2829 : {
2830 : // Queue variables
2831 : HlRateControlHistogramEntry *hl_rate_control_histogram_ptr_temp;
2832 : uint32_t queue_entry_index_head_temp;
2833 :
2834 : //SVT_LOG("\nOut:%d Slidings: ",picture_control_set_ptr->picture_number);
2835 0 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0) {
2836 : // Update the coded rate in the histogram queue
2837 0 : if (picture_control_set_ptr->picture_number >= sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue[sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number) {
2838 0 : queue_entry_index_head_temp = (int32_t)(picture_control_set_ptr->picture_number - sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue[sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_head_index]->picture_number);
2839 0 : queue_entry_index_head_temp += sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_head_index;
2840 0 : queue_entry_index_head_temp = (queue_entry_index_head_temp > HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH - 1) ?
2841 0 : queue_entry_index_head_temp - HIGH_LEVEL_RATE_CONTROL_HISTOGRAM_QUEUE_MAX_DEPTH :
2842 : queue_entry_index_head_temp;
2843 :
2844 0 : hl_rate_control_histogram_ptr_temp = (sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue[queue_entry_index_head_temp]);
2845 0 : if (hl_rate_control_histogram_ptr_temp->picture_number == picture_control_set_ptr->picture_number &&
2846 0 : hl_rate_control_histogram_ptr_temp->passed_to_hlrc) {
2847 0 : eb_block_on_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
2848 0 : hl_rate_control_histogram_ptr_temp->total_num_bits_coded = picture_control_set_ptr->total_num_bits;
2849 0 : hl_rate_control_histogram_ptr_temp->is_coded = EB_TRUE;
2850 0 : eb_release_mutex(sequence_control_set_ptr->encode_context_ptr->hl_rate_control_historgram_queue_mutex);
2851 : }
2852 : }
2853 : }
2854 0 : }
2855 : // rate control QP refinement
2856 0 : void rate_control_refinement(
2857 : PictureControlSet *picture_control_set_ptr,
2858 : SequenceControlSet *sequence_control_set_ptr,
2859 : RateControlIntervalParamContext *rate_control_param_ptr,
2860 : RateControlIntervalParamContext *prev_gop_rate_control_param_ptr,
2861 : RateControlIntervalParamContext *next_gop_rate_control_param_ptr) {
2862 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc && picture_control_set_ptr->picture_number != 0 && !prev_gop_rate_control_param_ptr->scene_change_in_gop) {
2863 0 : int16_t deltaApQp = (int16_t)prev_gop_rate_control_param_ptr->first_pic_actual_qp - (int16_t)prev_gop_rate_control_param_ptr->first_pic_pred_qp;
2864 0 : int64_t extraApBitRatio = (prev_gop_rate_control_param_ptr->first_pic_pred_bits != 0) ?
2865 0 : (((int64_t)prev_gop_rate_control_param_ptr->first_pic_actual_bits - (int64_t)prev_gop_rate_control_param_ptr->first_pic_pred_bits) * 100) / ((int64_t)prev_gop_rate_control_param_ptr->first_pic_pred_bits) :
2866 : 0;
2867 0 : extraApBitRatio += (int64_t)deltaApQp * 15;
2868 0 : if (extraApBitRatio > 200)
2869 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + 3;
2870 0 : else if (extraApBitRatio > 100)
2871 0 : picture_control_set_ptr->picture_qp = picture_control_set_ptr->picture_qp + 2;
2872 0 : else if (extraApBitRatio > 50)
2873 0 : picture_control_set_ptr->picture_qp++;
2874 : }
2875 :
2876 0 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc && picture_control_set_ptr->picture_number != 0) {
2877 0 : uint8_t qpIncAllowed = 3;
2878 0 : uint8_t qpDecAllowed = 4;
2879 0 : if (picture_control_set_ptr->parent_pcs_ptr->intra_selected_org_qp + 10 <= prev_gop_rate_control_param_ptr->first_pic_actual_qp)
2880 0 : qpDecAllowed = (uint8_t)(prev_gop_rate_control_param_ptr->first_pic_actual_qp - picture_control_set_ptr->parent_pcs_ptr->intra_selected_org_qp) >> 1;
2881 0 : if (picture_control_set_ptr->parent_pcs_ptr->intra_selected_org_qp >= prev_gop_rate_control_param_ptr->first_pic_actual_qp + 10)
2882 : {
2883 0 : qpIncAllowed = (uint8_t)(picture_control_set_ptr->parent_pcs_ptr->intra_selected_org_qp - prev_gop_rate_control_param_ptr->first_pic_actual_qp) * 2 / 3;
2884 0 : if (prev_gop_rate_control_param_ptr->first_pic_actual_qp <= 15)
2885 0 : qpIncAllowed += 5;
2886 0 : else if (prev_gop_rate_control_param_ptr->first_pic_actual_qp <= 20)
2887 0 : qpIncAllowed += 4;
2888 0 : else if (prev_gop_rate_control_param_ptr->first_pic_actual_qp <= 25)
2889 0 : qpIncAllowed += 3;
2890 : }
2891 0 : else if (prev_gop_rate_control_param_ptr->scene_change_in_gop)
2892 0 : qpIncAllowed = 5;
2893 0 : if (picture_control_set_ptr->parent_pcs_ptr->end_of_sequence_region) {
2894 0 : qpIncAllowed += 2;
2895 0 : qpDecAllowed += 4;
2896 : }
2897 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2898 : (uint32_t)MAX((int32_t)prev_gop_rate_control_param_ptr->first_pic_actual_qp - (int32_t)qpDecAllowed, 0),
2899 : (uint32_t)prev_gop_rate_control_param_ptr->first_pic_actual_qp + qpIncAllowed,
2900 : picture_control_set_ptr->picture_qp);
2901 : }
2902 :
2903 : // Scene change
2904 0 : if (picture_control_set_ptr->slice_type == I_SLICE && picture_control_set_ptr->picture_number != rate_control_param_ptr->first_poc) {
2905 0 : if (next_gop_rate_control_param_ptr->first_pic_actual_qp_assigned) {
2906 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2907 : (uint32_t)MAX((int32_t)next_gop_rate_control_param_ptr->first_pic_actual_qp - (int32_t)1, 0),
2908 : (uint32_t)next_gop_rate_control_param_ptr->first_pic_actual_qp + 8,
2909 : picture_control_set_ptr->picture_qp);
2910 : }
2911 : else {
2912 0 : if (rate_control_param_ptr->first_pic_actual_qp < 20) {
2913 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2914 : (uint32_t)MAX((int32_t)rate_control_param_ptr->first_pic_actual_qp - (int32_t)4, 0),
2915 : (uint32_t)rate_control_param_ptr->first_pic_actual_qp + 10,
2916 : picture_control_set_ptr->picture_qp);
2917 : }
2918 : else {
2919 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2920 : (uint32_t)MAX((int32_t)rate_control_param_ptr->first_pic_actual_qp - (int32_t)4, 0),
2921 : (uint32_t)rate_control_param_ptr->first_pic_actual_qp + 8,
2922 : picture_control_set_ptr->picture_qp);
2923 : }
2924 : }
2925 : }
2926 :
2927 0 : if (sequence_control_set_ptr->intra_period_length != -1 && picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels < 2 && (int32_t)picture_control_set_ptr->temporal_layer_index == 0 && picture_control_set_ptr->slice_type != I_SLICE) {
2928 0 : if (next_gop_rate_control_param_ptr->first_pic_actual_qp_assigned || next_gop_rate_control_param_ptr->was_used) {
2929 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2930 : (uint32_t)MAX((int32_t)next_gop_rate_control_param_ptr->first_pic_actual_qp - (int32_t)4, 0),
2931 : (uint32_t)picture_control_set_ptr->picture_qp,
2932 : picture_control_set_ptr->picture_qp);
2933 : }
2934 : else {
2935 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
2936 : (uint32_t)MAX((int32_t)rate_control_param_ptr->first_pic_actual_qp - (int32_t)4, 0),
2937 : (uint32_t)picture_control_set_ptr->picture_qp,
2938 : picture_control_set_ptr->picture_qp);
2939 : }
2940 : }
2941 0 : }
2942 : // initialize the rate control parameter at the beginning
2943 2 : void init_rc(
2944 : RateControlContext *context_ptr,
2945 : PictureControlSet *picture_control_set_ptr,
2946 : SequenceControlSet *sequence_control_set_ptr) {
2947 2 : context_ptr->high_level_rate_control_ptr->target_bit_rate = sequence_control_set_ptr->static_config.target_bit_rate;
2948 2 : context_ptr->high_level_rate_control_ptr->frame_rate = sequence_control_set_ptr->frame_rate;
2949 2 : context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_frame = (uint64_t)MAX((int64_t)1, (int64_t)((context_ptr->high_level_rate_control_ptr->target_bit_rate << RC_PRECISION) / context_ptr->high_level_rate_control_ptr->frame_rate));
2950 :
2951 2 : context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_sw = context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_frame * (sequence_control_set_ptr->static_config.look_ahead_distance + 1);
2952 2 : context_ptr->high_level_rate_control_ptr->bit_constraint_per_sw = context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_sw;
2953 :
2954 : #if RC_UPDATE_TARGET_RATE
2955 2 : context_ptr->high_level_rate_control_ptr->previous_updated_bit_constraint_per_sw = context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_sw;
2956 : #endif
2957 :
2958 2 : int32_t total_frame_in_interval = sequence_control_set_ptr->intra_period_length;
2959 2 : uint32_t gopPeriod = (1 << picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels);
2960 2 : context_ptr->frame_rate = sequence_control_set_ptr->frame_rate;
2961 66 : while (total_frame_in_interval >= 0) {
2962 64 : if (total_frame_in_interval % (gopPeriod) == 0)
2963 4 : context_ptr->frames_in_interval[0] ++;
2964 60 : else if (total_frame_in_interval % (gopPeriod >> 1) == 0)
2965 4 : context_ptr->frames_in_interval[1] ++;
2966 56 : else if (total_frame_in_interval % (gopPeriod >> 2) == 0)
2967 8 : context_ptr->frames_in_interval[2] ++;
2968 48 : else if (total_frame_in_interval % (gopPeriod >> 3) == 0)
2969 16 : context_ptr->frames_in_interval[3] ++;
2970 32 : else if (total_frame_in_interval % (gopPeriod >> 4) == 0)
2971 32 : context_ptr->frames_in_interval[4] ++;
2972 0 : else if (total_frame_in_interval % (gopPeriod >> 5) == 0)
2973 0 : context_ptr->frames_in_interval[5] ++;
2974 64 : total_frame_in_interval--;
2975 : }
2976 2 : if (sequence_control_set_ptr->static_config.rate_control_mode == 2) { // VBR
2977 0 : context_ptr->virtual_buffer_size = (((uint64_t)sequence_control_set_ptr->static_config.target_bit_rate * 3) << RC_PRECISION) / (context_ptr->frame_rate);
2978 0 : context_ptr->rate_average_periodin_frames = (uint64_t)sequence_control_set_ptr->static_config.intra_period_length + 1;
2979 0 : context_ptr->virtual_buffer_level_initial_value = context_ptr->virtual_buffer_size >> 1;
2980 0 : context_ptr->virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
2981 0 : context_ptr->previous_virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
2982 0 : context_ptr->vb_fill_threshold1 = (context_ptr->virtual_buffer_size * 6) >> 3;
2983 0 : context_ptr->vb_fill_threshold2 = (context_ptr->virtual_buffer_size << 3) >> 3;
2984 0 : context_ptr->base_layer_frames_avg_qp = sequence_control_set_ptr->qp;
2985 0 : context_ptr->base_layer_intra_frames_avg_qp = sequence_control_set_ptr->qp;
2986 : }
2987 2 : else if (sequence_control_set_ptr->static_config.rate_control_mode == 3) {
2988 0 : context_ptr->virtual_buffer_size = ((uint64_t)sequence_control_set_ptr->static_config.target_bit_rate);// vbv_buf_size);
2989 0 : context_ptr->rate_average_periodin_frames = (uint64_t)sequence_control_set_ptr->static_config.intra_period_length + 1;
2990 0 : context_ptr->virtual_buffer_level_initial_value = context_ptr->virtual_buffer_size >> 1;
2991 0 : context_ptr->virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
2992 0 : context_ptr->previous_virtual_buffer_level = context_ptr->virtual_buffer_size >> 1;
2993 0 : context_ptr->vb_fill_threshold1 = context_ptr->virtual_buffer_level_initial_value + (context_ptr->virtual_buffer_size / 4);
2994 0 : context_ptr->vb_fill_threshold2 = context_ptr->virtual_buffer_level_initial_value + (context_ptr->virtual_buffer_size / 3);
2995 0 : context_ptr->base_layer_frames_avg_qp = sequence_control_set_ptr->qp;
2996 0 : context_ptr->base_layer_intra_frames_avg_qp = sequence_control_set_ptr->qp;
2997 : }
2998 :
2999 164 : for (uint32_t base_qp = 0; base_qp < MAX_REF_QP_NUM; base_qp++) {
3000 162 : if (base_qp < 64) {
3001 128 : context_ptr->qp_scaling_map_I_SLICE[base_qp] = qp_scaling_calc(
3002 : sequence_control_set_ptr,
3003 : I_SLICE,
3004 : 0,
3005 : base_qp);
3006 : }
3007 : else
3008 34 : context_ptr->qp_scaling_map_I_SLICE[base_qp] = (uint32_t)CLIP3(0, 63, (int)base_qp - (63 - (int)context_ptr->qp_scaling_map_I_SLICE[63]));
3009 972 : for (uint32_t temporal_layer_index = 0; temporal_layer_index < sequence_control_set_ptr->static_config.hierarchical_levels+1; temporal_layer_index++) {
3010 810 : if (base_qp < 64) {
3011 640 : context_ptr->qp_scaling_map[temporal_layer_index][base_qp] = qp_scaling_calc(
3012 : sequence_control_set_ptr,
3013 : 0,
3014 : temporal_layer_index,
3015 : base_qp);
3016 : }
3017 : else
3018 170 : context_ptr->qp_scaling_map[temporal_layer_index][base_qp] = (uint32_t)CLIP3(0, 63, (int)base_qp - (63 - (int)context_ptr->qp_scaling_map[temporal_layer_index][63]));
3019 : }
3020 : }
3021 2 : }
3022 :
3023 : #define MAX_Q_INDEX 255
3024 : #define MIN_Q_INDEX 0
3025 :
3026 : extern int16_t eb_av1_ac_quant_Q3(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
3027 : // These functions use formulaic calculations to make playing with the
3028 : // quantizer tables easier. If necessary they can be replaced by lookup
3029 : // tables if and when things settle down in the experimental bitstream
3030 :
3031 183474 : double eb_av1_convert_qindex_to_q(int32_t qindex, AomBitDepth bit_depth) {
3032 : // Convert the index to a real Q value (scaled down to match old Q values)
3033 183474 : switch (bit_depth) {
3034 183474 : case AOM_BITS_8: return eb_av1_ac_quant_Q3(qindex, 0, bit_depth) / 4.0;
3035 0 : case AOM_BITS_10: return eb_av1_ac_quant_Q3(qindex, 0, bit_depth) / 16.0;
3036 0 : case AOM_BITS_12: return eb_av1_ac_quant_Q3(qindex, 0, bit_depth) / 64.0;
3037 0 : default:
3038 0 : assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
3039 : return -1.0;
3040 : }
3041 : }
3042 786 : int32_t eb_av1_compute_qdelta(double qstart, double qtarget,
3043 : AomBitDepth bit_depth) {
3044 786 : int32_t start_index = MAX_Q_INDEX;
3045 786 : int32_t target_index = MAX_Q_INDEX;
3046 : int32_t i;
3047 :
3048 : // Convert the average q value to an index.
3049 101314 : for (i = MIN_Q_INDEX; i < MAX_Q_INDEX; ++i) {
3050 101302 : start_index = i;
3051 101302 : if (eb_av1_convert_qindex_to_q(i, bit_depth) >= qstart) break;
3052 : }
3053 :
3054 : // Convert the q target to an index
3055 80674 : for (i = MIN_Q_INDEX; i < MAX_Q_INDEX; ++i) {
3056 80672 : target_index = i;
3057 80672 : if (eb_av1_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
3058 : }
3059 :
3060 786 : return target_index - start_index;
3061 : }
3062 : // calculate the QP based on the QP scaling
3063 768 : uint32_t qp_scaling_calc(
3064 : SequenceControlSet *sequence_control_set_ptr,
3065 : EB_SLICE slice_type,
3066 : uint32_t temporal_layer_index,
3067 : uint32_t base_qp)
3068 : {
3069 : // AMIR to fix
3070 768 : uint32_t scaled_qp = 0;
3071 : int base_qindex;
3072 :
3073 768 : const double delta_rate_new[2][6] =
3074 : { { 0.40, 0.7, 0.85, 1.0, 1.0, 1.0 },
3075 : { 0.35, 0.6, 0.8, 0.9, 1.0, 1.0 } };
3076 :
3077 768 : int qindex = quantizer_to_qindex[base_qp];
3078 768 : const double q = eb_av1_convert_qindex_to_q(qindex, (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
3079 : int delta_qindex;
3080 :
3081 768 : if (slice_type == I_SLICE) {
3082 128 : delta_qindex = eb_av1_compute_qdelta(
3083 : q,
3084 : q* 0.25,
3085 128 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
3086 : }
3087 : else {
3088 640 : delta_qindex = eb_av1_compute_qdelta(
3089 : q,
3090 640 : q* delta_rate_new[sequence_control_set_ptr->static_config.hierarchical_levels == 4][temporal_layer_index], // RC does not support 5L
3091 : //q* delta_rate_new[0][temporal_layer_index], // RC does not support 5L
3092 640 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
3093 : }
3094 :
3095 768 : base_qindex = MAX(qindex + delta_qindex, MIN_Q_INDEX);
3096 768 : scaled_qp = (uint32_t)(base_qindex) >> 2;
3097 :
3098 768 : return scaled_qp;
3099 : }
3100 : typedef struct {
3101 : // Rate targetting variables
3102 : int base_frame_target; // A baseline frame target before adjustment
3103 : // for previous under or over shoot.
3104 : int this_frame_target; // Actual frame target after rc adjustment.
3105 : int projected_frame_size;
3106 : int sb64_target_rate;
3107 : int last_q[FRAME_TYPES]; // Separate values for Intra/Inter
3108 : int last_boosted_qindex; // Last boosted GF/KF/ARF q
3109 : int last_kf_qindex; // Q index of the last key frame coded.
3110 :
3111 : int gfu_boost;
3112 : int kf_boost;
3113 :
3114 : // double rate_correction_factors[RATE_FACTOR_LEVELS];
3115 :
3116 : int frames_since_golden;
3117 : int frames_till_gf_update_due;
3118 : int min_gf_interval;
3119 : int max_gf_interval;
3120 : int static_scene_max_gf_interval;
3121 : int baseline_gf_interval;
3122 : int constrained_gf_group;
3123 : int frames_to_key;
3124 : int frames_since_key;
3125 : int this_key_frame_forced;
3126 : int next_key_frame_forced;
3127 : int source_alt_ref_pending;
3128 : int source_alt_ref_active;
3129 : int is_src_frame_alt_ref;
3130 : int sframe_due;
3131 :
3132 : // Length of the bi-predictive frame group interval
3133 : int bipred_group_interval;
3134 :
3135 : // NOTE: Different types of frames may have different bits allocated
3136 : // accordingly, aiming to achieve the overall optimal RD performance.
3137 : int is_bwd_ref_frame;
3138 : int is_last_bipred_frame;
3139 : int is_bipred_frame;
3140 : int is_src_frame_ext_arf;
3141 :
3142 : int avg_frame_bandwidth; // Average frame size target for clip
3143 : int min_frame_bandwidth; // Minimum allocation used for any frame
3144 : int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
3145 :
3146 : int ni_av_qi;
3147 : int ni_tot_qi;
3148 : int ni_frames;
3149 : int avg_frame_qindex[FRAME_TYPES];
3150 : double tot_q;
3151 : double avg_q;
3152 :
3153 : int64_t buffer_level;
3154 : int64_t bits_off_target;
3155 : int64_t vbr_bits_off_target;
3156 : int64_t vbr_bits_off_target_fast;
3157 :
3158 : int decimation_factor;
3159 : int decimation_count;
3160 :
3161 : int rolling_target_bits;
3162 : int rolling_actual_bits;
3163 :
3164 : int long_rolling_target_bits;
3165 : int long_rolling_actual_bits;
3166 :
3167 : int rate_error_estimate;
3168 :
3169 : int64_t total_actual_bits;
3170 : int64_t total_target_bits;
3171 : int64_t total_target_vs_actual;
3172 :
3173 : int worst_quality;
3174 : int best_quality;
3175 :
3176 : int64_t starting_buffer_level;
3177 : int64_t optimal_buffer_level;
3178 : int64_t maximum_buffer_size;
3179 :
3180 : // rate control history for last frame(1) and the frame before(2).
3181 : // -1: undershot
3182 : // 1: overshoot
3183 : // 0: not initialized.
3184 : int rc_1_frame;
3185 : int rc_2_frame;
3186 : int q_1_frame;
3187 : int q_2_frame;
3188 :
3189 : // Auto frame-scaling variables.
3190 : // int rf_level_maxq[RATE_FACTOR_LEVELS];
3191 : float_t arf_boost_factor;
3192 : // Q index used for ALT frame
3193 : int arf_q;
3194 : } RATE_CONTROL;
3195 : #define STATIC_MOTION_THRESH 95
3196 :
3197 : enum {
3198 : INTER_NORMAL = 0,
3199 : INTER_LOW = 1,
3200 : INTER_HIGH = 2,
3201 : GF_ARF_LOW = 3,
3202 : GF_ARF_STD = 4,
3203 : KF_STD = 5,
3204 : RATE_FACTOR_LEVELS = 6
3205 : } RATE_FACTOR_LEVEL;
3206 :
3207 : enum {
3208 : KF_UPDATE = 0,
3209 : LF_UPDATE = 1,
3210 : GF_UPDATE = 2,
3211 : ARF_UPDATE = 3,
3212 : OVERLAY_UPDATE = 4,
3213 : BRF_UPDATE = 5, // Backward Reference Frame
3214 : LAST_BIPRED_UPDATE = 6, // Last Bi-predictive Frame
3215 : BIPRED_UPDATE = 7, // Bi-predictive Frame, but not the last one
3216 : INTNL_OVERLAY_UPDATE = 8, // Internal Overlay Frame
3217 : INTNL_ARF_UPDATE = 9, // Internal Altref Frame (candidate for ALTREF2)
3218 : FRAME_UPDATE_TYPES = 10
3219 : } FRAME_UPDATE_TYPE;
3220 :
3221 : // that are not marked as coded with 0,0 motion in the first pass.
3222 : #define FAST_MOVING_KF_GROUP_THRESH 5
3223 : #define MEDIUM_MOVING_KF_GROUP_THRESH 30
3224 : #define STATIC_KF_GROUP_THRESH 70
3225 : #define MAX_QPS_COMP_I 150
3226 : #define MAX_QPS_COMP_I_LR 42
3227 : #define MAX_QPS_COMP_NONI 300
3228 : #define HIGH_QPS_COMP_THRESHOLD 80
3229 : #define LOW_QPS_COMP_THRESHOLD 40
3230 : #define HIGH_FILTERED_THRESHOLD (4<<8) // 8 bit precision
3231 : #define LOW_FILTERED_THRESHOLD (2<<8) // 8 bit precision
3232 : #define QPS_SW_THRESH 8
3233 :
3234 : #if TWO_PASS
3235 : #if TWO_PASS_IMPROVEMENT
3236 : #define MAX_REF_AREA_I 50 // Max ref area for I slice
3237 : #define MAX_REF_AREA_NONI 50 // Max ref area for Non I slice
3238 : #define MAX_REF_AREA_NONI_LOW_RES 40 // Max ref area for Non I slice in low resolution
3239 : #else
3240 : #define MAX_REF_AREA_I 45 // Max ref area for I slice
3241 : #define MAX_REF_AREA_NONI 40 // Max ref area for Non I slice
3242 : #define MAX_REF_AREA_NONI_LOW_RES 30 // Max ref area for Non I slice in low resolution
3243 : #endif
3244 : #define REF_AREA_DIF_THRESHOLD 10 // Difference threshold for ref area between two frames
3245 : #define REF_AREA_LOW_THRESHOLD 8 // Low threshold for ref area
3246 : #define REF_AREA_MED_THRESHOLD 16 // Medium threshold for ref area
3247 :
3248 : #define ME_SAD_LOW_THRESHOLD1 15 // Low Sad threshold1 for me distortion (very low)
3249 : #define ME_SAD_LOW_THRESHOLD2 25 // Low Sad threshold2 for me distortion (low)
3250 : #define ME_SAD_HIGH_THRESHOLD 80 // High Sad threshold2 for me distortion (high)
3251 : #endif
3252 :
3253 : #define ASSIGN_MINQ_TABLE(bit_depth, name) \
3254 : do { \
3255 : switch (bit_depth) { \
3256 : case AOM_BITS_8: name = name##_8; break; \
3257 : case AOM_BITS_10: name = name##_10; break; \
3258 : case AOM_BITS_12: name = name##_12; break; \
3259 : default: \
3260 : assert(0 && \
3261 : "bit_depth should be AOM_BITS_8, AOM_BITS_10" \
3262 : " or AOM_BITS_12"); \
3263 : name = NULL; \
3264 : } \
3265 : } while (0)
3266 :
3267 :
3268 : static int kf_low_motion_minq_8[QINDEX_RANGE] = {
3269 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3270 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3271 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3272 : 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3273 : 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
3274 : 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
3275 : 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
3276 : 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14,
3277 : 14, 15, 15, 15, 16, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19,
3278 : 20, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26,
3279 : 26, 27, 27, 28, 28, 29, 30, 30, 31, 31, 32, 32, 33, 34, 34,
3280 : 35, 36, 36, 37, 37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45,
3281 : 45, 46, 47, 48, 49, 50, 51, 51, 52, 54, 55, 56, 57, 58, 59,
3282 : 61, 62, 63, 64, 67, 68, 69, 71, 73, 74, 76, 77, 80, 81, 83,
3283 : 85, 87, 90, 91, 94, 96, 99, 102, 104, 107, 108, 110, 113, 114, 116,
3284 : 119, 121, 122, 125, 128, 130, 133, 135, 138, 141, 144, 147, 150, 152, 155,
3285 : 158, 161, 165, 168, 171, 174, 177, 180, 184, 187, 190, 194, 197, 201, 205,
3286 : 208};
3287 :
3288 : static int kf_high_motion_minq_8[QINDEX_RANGE] = {
3289 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3,
3290 : 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9,
3291 : 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 16,
3292 : 16, 16, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21, 22,
3293 : 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28,
3294 : 28, 29, 29, 30, 30, 30, 31, 31, 32, 32, 32, 33, 33, 34, 34,
3295 : 34, 35, 35, 36, 36, 36, 37, 38, 39, 39, 40, 41, 42, 42, 43,
3296 : 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, 54,
3297 : 55, 56, 57, 58, 59, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
3298 : 71, 72, 73, 74, 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89,
3299 : 90, 92, 93, 95, 96, 97, 97, 98, 99, 100, 100, 101, 102, 103, 104,
3300 : 105, 106, 107, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
3301 : 119, 120, 121, 121, 122, 123, 124, 124, 125, 126, 128, 128, 129, 130, 131,
3302 : 131, 132, 134, 135, 136, 137, 138, 139, 140, 141, 143, 143, 144, 146, 146,
3303 : 147, 149, 150, 151, 152, 153, 155, 156, 158, 158, 160, 161, 163, 164, 166,
3304 : 166, 168, 170, 171, 173, 174, 176, 178, 179, 181, 183, 185, 187, 189, 191,
3305 : 193, 195, 197, 200, 201, 204, 206, 209, 212, 214, 216, 219, 222, 224, 227,
3306 : 230};
3307 :
3308 : static int arfgf_low_motion_minq_8[QINDEX_RANGE] = {
3309 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3310 : 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 3, 3, 3, 4, 4,
3311 : 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
3312 : 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12,
3313 : 13, 13, 13, 13, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
3314 : 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20, 20, 20, 20,
3315 : 21, 21, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
3316 : 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34,
3317 : 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 41, 42, 43, 43, 44,
3318 : 45, 45, 46, 47, 48, 49, 49, 50, 51, 52, 53, 54, 54, 55, 56,
3319 : 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
3320 : 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 89,
3321 : 90, 91, 92, 94, 95, 96, 97, 97, 98, 100, 100, 101, 102, 102, 103,
3322 : 105, 106, 106, 107, 109, 110, 110, 112, 113, 114, 116, 116, 118, 119, 120,
3323 : 122, 123, 125, 125, 127, 128, 130, 132, 133, 134, 135, 137, 139, 140, 141,
3324 : 143, 145, 146, 148, 150, 152, 154, 155, 158, 160, 162, 164, 166, 168, 171,
3325 : 173, 176, 178, 181, 183, 186, 188, 191, 194, 197, 200, 203, 206, 210, 213,
3326 : 216};
3327 :
3328 : static int arfgf_high_motion_minq_8[QINDEX_RANGE] = {
3329 : 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 3, 4, 4, 5,
3330 : 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,
3331 : 13, 14, 14, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
3332 : 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
3333 : 29, 29, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36,
3334 : 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44,
3335 : 44, 45, 45, 46, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
3336 : 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 68, 69,
3337 : 70, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 87, 88, 89,
3338 : 91, 92, 93, 95, 96, 97, 98, 99, 100, 100, 101, 102, 103, 104, 105,
3339 : 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
3340 : 121, 122, 123, 123, 124, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133,
3341 : 134, 135, 136, 137, 138, 139, 139, 140, 141, 142, 144, 144, 145, 146, 147,
3342 : 148, 149, 151, 151, 152, 153, 155, 156, 156, 157, 159, 160, 161, 162, 163,
3343 : 164, 166, 167, 169, 169, 170, 172, 173, 175, 176, 178, 179, 180, 181, 183,
3344 : 184, 186, 188, 189, 191, 192, 194, 196, 197, 199, 201, 202, 204, 206, 209,
3345 : 210, 212, 214, 217, 218, 220, 223, 225, 228, 230, 232, 234, 237, 239, 242,
3346 : 245};
3347 : /*
3348 : static int inter_minq_8[QINDEX_RANGE] = {
3349 : 0, 0, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12,
3350 : 13, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26,
3351 : 26, 27, 28, 29, 30, 31, 32, 33, 33, 34, 35, 36, 37, 38, 39,
3352 : 40, 40, 41, 42, 43, 44, 45, 46, 47, 47, 48, 49, 50, 51, 52,
3353 : 53, 53, 54, 55, 56, 57, 58, 59, 59, 60, 61, 62, 63, 64, 65,
3354 : 65, 66, 67, 68, 69, 70, 71, 71, 72, 73, 74, 75, 76, 77, 77,
3355 : 78, 79, 80, 81, 82, 83, 84, 86, 88, 89, 91, 93, 94, 96, 97,
3356 : 97, 98, 99, 100, 101, 102, 102, 103, 104, 105, 106, 107, 107, 108, 109,
3357 : 110, 111, 112, 114, 115, 116, 117, 119, 120, 121, 122, 122, 123, 124, 125,
3358 : 126, 127, 127, 128, 129, 131, 132, 133, 134, 135, 136, 137, 138, 139, 139,
3359 : 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
3360 : 155, 156, 157, 157, 158, 159, 161, 161, 162, 163, 164, 165, 166, 167, 168,
3361 : 169, 170, 171, 172, 173, 174, 175, 176, 176, 177, 178, 179, 180, 181, 182,
3362 : 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 196,
3363 : 197, 199, 199, 200, 201, 203, 203, 205, 206, 207, 208, 209, 210, 211, 212,
3364 : 213, 214, 215, 216, 217, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230,
3365 : 231, 232, 234, 235, 236, 238, 239, 240, 242, 243, 245, 246, 248, 250, 251,
3366 : 253};
3367 :
3368 : static int rtc_minq_8[QINDEX_RANGE] = {
3369 : 0, 0, 0, 0, 0, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8,
3370 : 9, 9, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18,
3371 : 19, 20, 20, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 28, 28,
3372 : 29, 30, 31, 31, 32, 33, 33, 34, 35, 35, 36, 37, 37, 38, 39,
3373 : 39, 40, 41, 41, 42, 42, 43, 44, 44, 45, 46, 46, 47, 48, 48,
3374 : 49, 50, 50, 51, 52, 52, 53, 54, 54, 55, 56, 56, 57, 58, 58,
3375 : 59, 60, 60, 61, 61, 62, 63, 65, 66, 67, 69, 70, 71, 72, 74,
3376 : 75, 76, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89, 90, 91, 93,
3377 : 94, 96, 97, 98, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
3378 : 109, 110, 110, 112, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123,
3379 : 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
3380 : 139, 140, 141, 142, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
3381 : 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 162, 163, 164, 165, 166,
3382 : 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
3383 : 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
3384 : 197, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
3385 : 216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
3386 : 241};
3387 : */
3388 :
3389 : static int kf_low_motion_minq_10[QINDEX_RANGE] = {
3390 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3391 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3392 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3393 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 11, 11, 11, 11,
3394 : 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 14,
3395 : 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16,
3396 : 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19,
3397 : 19, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23,
3398 : 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28,
3399 : 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 33, 33, 34, 34,
3400 : 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41,
3401 : 42, 42, 43, 44, 44, 45, 46, 46, 47, 47, 48, 49, 49, 50, 51,
3402 : 51, 52, 53, 54, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
3403 : 66, 67, 68, 69, 71, 72, 73, 75, 76, 77, 79, 81, 83, 84, 86,
3404 : 88, 90, 92, 94, 96, 98, 101, 104, 106, 108, 109, 111, 114, 115, 117,
3405 : 119, 122, 123, 126, 128, 131, 134, 136, 139, 142, 145, 147, 150, 153, 156,
3406 : 159, 162, 165, 168, 171, 174, 177, 180, 184, 187, 190, 194, 197, 202, 205,
3407 : 208};
3408 :
3409 : static int kf_high_motion_minq_10[QINDEX_RANGE] = {
3410 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3411 : 0, 0, 0, 0, 0, 0, 11, 11, 11, 12, 13, 13, 14, 14, 15,
3412 : 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
3413 : 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 29,
3414 : 29, 29, 30, 30, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 35,
3415 : 35, 35, 36, 36, 37, 37, 37, 38, 38, 39, 39, 39, 40, 40, 41,
3416 : 41, 41, 42, 42, 42, 43, 43, 44, 45, 45, 46, 47, 48, 48, 49,
3417 : 50, 50, 51, 52, 52, 53, 54, 54, 55, 56, 56, 57, 58, 58, 59,
3418 : 60, 61, 62, 63, 64, 64, 66, 67, 67, 69, 69, 70, 71, 72, 73,
3419 : 74, 75, 76, 77, 79, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91,
3420 : 92, 94, 95, 96, 97, 97, 98, 99, 100, 101, 101, 102, 103, 104, 105,
3421 : 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118,
3422 : 119, 120, 121, 122, 123, 123, 124, 125, 125, 126, 128, 129, 129, 130, 131,
3423 : 132, 133, 134, 135, 136, 137, 139, 139, 140, 141, 143, 143, 144, 146, 147,
3424 : 147, 149, 150, 151, 152, 153, 155, 156, 158, 159, 160, 161, 163, 164, 166,
3425 : 166, 168, 170, 171, 173, 174, 176, 178, 179, 181, 184, 185, 187, 189, 191,
3426 : 193, 195, 197, 200, 201, 204, 206, 209, 212, 214, 216, 219, 222, 224, 227,
3427 : 230};
3428 :
3429 : static int arfgf_low_motion_minq_10[QINDEX_RANGE] = {
3430 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3431 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
3432 : 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 16,
3433 : 16, 16, 17, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20,
3434 : 21, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24, 25,
3435 : 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28, 28,
3436 : 29, 29, 29, 30, 30, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34,
3437 : 35, 35, 36, 36, 37, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41,
3438 : 41, 42, 42, 43, 44, 44, 45, 46, 46, 47, 48, 48, 49, 49, 50,
3439 : 50, 51, 52, 52, 53, 54, 55, 56, 56, 57, 58, 59, 59, 60, 61,
3440 : 62, 62, 63, 64, 65, 66, 67, 68, 69, 69, 70, 72, 72, 73, 74,
3441 : 75, 77, 77, 78, 79, 80, 82, 83, 84, 85, 86, 87, 88, 90, 91,
3442 : 92, 93, 94, 95, 96, 97, 98, 98, 99, 101, 101, 102, 103, 103, 104,
3443 : 106, 106, 107, 108, 110, 110, 111, 113, 114, 114, 116, 117, 119, 120, 121,
3444 : 122, 123, 125, 126, 128, 129, 131, 132, 133, 135, 136, 137, 139, 140, 142,
3445 : 144, 145, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 169, 171,
3446 : 173, 176, 178, 181, 184, 186, 189, 191, 194, 197, 200, 203, 206, 210, 213,
3447 : 216};
3448 :
3449 : static int arfgf_high_motion_minq_10[QINDEX_RANGE] = {
3450 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3451 : 0, 0, 10, 11, 11, 12, 13, 13, 14, 14, 15, 16, 16, 17, 18,
3452 : 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 26, 26,
3453 : 27, 27, 28, 28, 29, 30, 30, 30, 31, 32, 32, 33, 33, 34, 34,
3454 : 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42,
3455 : 42, 42, 43, 44, 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49,
3456 : 49, 50, 50, 51, 51, 52, 52, 53, 54, 55, 56, 57, 58, 59, 60,
3457 : 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69, 70, 71, 72, 72,
3458 : 73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 89, 90, 91,
3459 : 92, 94, 95, 96, 97, 98, 99, 99, 100, 101, 102, 103, 104, 105, 105,
3460 : 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121,
3461 : 121, 122, 123, 124, 125, 125, 126, 127, 128, 129, 130, 130, 131, 132, 133,
3462 : 134, 135, 136, 137, 138, 139, 140, 140, 141, 142, 144, 145, 145, 146, 147,
3463 : 148, 149, 151, 152, 152, 153, 155, 156, 156, 157, 159, 160, 161, 163, 163,
3464 : 164, 166, 167, 169, 170, 170, 172, 173, 175, 176, 178, 179, 181, 181, 183,
3465 : 184, 186, 188, 189, 191, 192, 194, 196, 197, 199, 201, 202, 204, 206, 209,
3466 : 210, 212, 214, 217, 218, 220, 223, 225, 228, 230, 232, 234, 237, 240, 242,
3467 : 245};
3468 : /*
3469 : static int inter_minq_10[QINDEX_RANGE] = {
3470 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 12, 13,
3471 : 14, 15, 16, 17, 18, 19, 20, 20, 21, 22, 23, 24, 25, 26, 27,
3472 : 28, 29, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 39, 39, 40,
3473 : 41, 42, 43, 44, 44, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53,
3474 : 54, 55, 56, 57, 58, 58, 59, 60, 61, 62, 62, 63, 64, 65, 66,
3475 : 67, 68, 69, 69, 70, 71, 72, 73, 73, 74, 75, 76, 77, 78, 79,
3476 : 79, 80, 81, 82, 83, 84, 85, 87, 88, 90, 92, 93, 95, 96, 97,
3477 : 98, 99, 99, 100, 101, 102, 103, 104, 104, 105, 106, 107, 108, 109, 109,
3478 : 110, 111, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123, 124, 125,
3479 : 126, 127, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
3480 : 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
3481 : 155, 156, 157, 158, 158, 160, 161, 161, 162, 163, 164, 165, 166, 167, 168,
3482 : 169, 170, 171, 172, 173, 174, 175, 176, 177, 177, 178, 179, 180, 181, 182,
3483 : 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 196,
3484 : 197, 199, 199, 200, 201, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
3485 : 213, 214, 215, 216, 218, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230,
3486 : 231, 232, 234, 235, 236, 238, 239, 240, 242, 243, 245, 246, 248, 250, 251,
3487 : 253};
3488 :
3489 : static int rtc_minq_10[QINDEX_RANGE] = {
3490 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,
3491 : 11, 12, 13, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22,
3492 : 22, 23, 24, 24, 25, 26, 27, 28, 28, 29, 29, 30, 31, 32, 32,
3493 : 33, 34, 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 41, 42, 42,
3494 : 43, 44, 44, 45, 46, 46, 47, 48, 48, 49, 50, 50, 51, 51, 52,
3495 : 53, 53, 54, 55, 55, 56, 56, 57, 58, 58, 59, 60, 60, 61, 62,
3496 : 62, 63, 63, 64, 64, 65, 67, 68, 69, 70, 71, 72, 74, 75, 76,
3497 : 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 93, 94,
3498 : 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 105, 106, 107, 108,
3499 : 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 123, 124,
3500 : 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
3501 : 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 151, 152,
3502 : 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 163, 164, 165, 166,
3503 : 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
3504 : 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
3505 : 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
3506 : 216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
3507 : 241};
3508 : */
3509 :
3510 : static int kf_low_motion_minq_12[QINDEX_RANGE] = {
3511 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3512 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3513 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3514 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 13,
3515 : 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16,
3516 : 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18,
3517 : 18, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
3518 : 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25,
3519 : 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30,
3520 : 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 35, 35, 35, 36,
3521 : 36, 37, 37, 38, 38, 39, 39, 39, 40, 40, 41, 41, 42, 42, 43,
3522 : 44, 44, 45, 45, 46, 46, 47, 48, 48, 49, 49, 50, 51, 51, 52,
3523 : 53, 53, 54, 55, 56, 56, 57, 58, 59, 59, 60, 61, 62, 63, 64,
3524 : 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80,
3525 : 82, 83, 85, 86, 88, 90, 91, 93, 95, 96, 97, 99, 100, 101, 102,
3526 : 104, 105, 106, 108, 110, 111, 113, 115, 117, 119, 121, 122, 124, 126, 128,
3527 : 130, 132, 134, 136, 138, 140, 142, 144, 147, 149, 152, 154, 156, 159, 161,
3528 : 163};
3529 :
3530 : static int kf_high_motion_minq_12[QINDEX_RANGE] = {
3531 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3532 : 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 14, 15, 15, 16, 16,
3533 : 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 23,
3534 : 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 28, 29, 29, 30, 30,
3535 : 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 35, 35, 35, 36, 36,
3536 : 37, 37, 37, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42,
3537 : 42, 43, 43, 43, 44, 44, 45, 45, 46, 47, 47, 48, 49, 49, 50,
3538 : 51, 51, 52, 53, 53, 54, 55, 55, 56, 57, 57, 58, 59, 59, 60,
3539 : 61, 62, 63, 64, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
3540 : 75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 88, 89, 90, 91,
3541 : 92, 94, 95, 96, 97, 98, 98, 99, 100, 101, 101, 102, 103, 104, 105,
3542 : 106, 107, 107, 108, 109, 110, 111, 112, 113, 114, 115, 115, 116, 117, 118,
3543 : 119, 120, 121, 122, 123, 123, 124, 125, 125, 126, 127, 128, 128, 129, 130,
3544 : 131, 132, 132, 133, 134, 135, 136, 137, 137, 138, 139, 139, 140, 141, 142,
3545 : 142, 143, 144, 145, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154,
3546 : 155, 155, 156, 157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 170,
3547 : 171, 172, 173, 175, 176, 178, 179, 181, 183, 184, 186, 188, 190, 191, 193,
3548 : 195};
3549 :
3550 : static int arfgf_low_motion_minq_12[QINDEX_RANGE] = {
3551 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3552 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3553 : 0, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17,
3554 : 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22,
3555 : 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
3556 : 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30,
3557 : 31, 31, 31, 31, 32, 32, 32, 33, 33, 34, 34, 35, 35, 35, 36,
3558 : 36, 37, 37, 38, 38, 39, 39, 39, 40, 40, 41, 41, 42, 42, 42,
3559 : 43, 43, 44, 45, 45, 46, 46, 47, 48, 48, 49, 49, 50, 51, 51,
3560 : 52, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62,
3561 : 63, 63, 64, 65, 66, 67, 68, 69, 70, 70, 71, 72, 73, 74, 75,
3562 : 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91,
3563 : 92, 94, 95, 96, 96, 97, 98, 98, 99, 100, 100, 101, 102, 102, 103,
3564 : 104, 105, 105, 106, 107, 108, 108, 109, 110, 111, 111, 112, 113, 114, 115,
3565 : 115, 116, 117, 118, 119, 120, 121, 122, 122, 123, 124, 124, 125, 126, 127,
3566 : 128, 129, 129, 130, 131, 132, 134, 135, 136, 137, 138, 139, 141, 142, 143,
3567 : 144, 146, 147, 149, 151, 152, 154, 155, 157, 159, 161, 163, 165, 167, 169,
3568 : 171};
3569 :
3570 : static int arfgf_high_motion_minq_12[QINDEX_RANGE] = {
3571 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3572 : 0, 0, 0, 0, 0, 13, 14, 14, 15, 16, 16, 17, 17, 18, 19,
3573 : 19, 20, 20, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 27,
3574 : 28, 28, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35,
3575 : 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43,
3576 : 43, 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 49, 50,
3577 : 50, 51, 51, 52, 52, 53, 53, 54, 55, 56, 57, 58, 59, 59, 60,
3578 : 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 70, 71, 71, 72, 73,
3579 : 74, 75, 77, 78, 79, 80, 82, 83, 84, 85, 87, 88, 89, 90, 92,
3580 : 93, 94, 95, 96, 97, 98, 99, 100, 101, 101, 102, 103, 104, 105, 106,
3581 : 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 119, 120, 121,
3582 : 122, 122, 123, 124, 125, 125, 126, 127, 128, 129, 130, 131, 132, 132, 133,
3583 : 134, 135, 136, 137, 138, 139, 140, 140, 141, 142, 143, 144, 144, 145, 146,
3584 : 147, 148, 149, 150, 150, 151, 152, 153, 154, 154, 155, 156, 157, 158, 158,
3585 : 159, 160, 161, 162, 163, 163, 164, 165, 166, 167, 168, 169, 170, 170, 171,
3586 : 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 187,
3587 : 188, 189, 190, 192, 193, 194, 196, 197, 199, 200, 202, 203, 205, 207, 208,
3588 : 210};
3589 :
3590 : /*
3591 : static int inter_minq_12[QINDEX_RANGE] = {
3592 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13,
3593 : 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 25, 26, 27,
3594 : 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 40,
3595 : 41, 42, 43, 44, 45, 46, 47, 47, 48, 49, 50, 51, 52, 53, 53,
3596 : 54, 55, 56, 57, 58, 59, 59, 60, 61, 62, 63, 64, 65, 65, 66,
3597 : 67, 68, 69, 70, 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79,
3598 : 80, 80, 81, 82, 83, 84, 85, 87, 89, 90, 92, 93, 95, 96, 97,
3599 : 98, 99, 99, 100, 101, 102, 103, 104, 104, 105, 106, 107, 108, 109, 109,
3600 : 110, 111, 113, 114, 115, 116, 118, 119, 120, 121, 122, 123, 123, 124, 125,
3601 : 126, 127, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
3602 : 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
3603 : 155, 156, 157, 158, 158, 160, 161, 161, 162, 163, 164, 165, 166, 167, 168,
3604 : 169, 170, 171, 172, 173, 174, 175, 176, 177, 177, 178, 179, 180, 181, 182,
3605 : 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 196,
3606 : 197, 199, 199, 200, 201, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
3607 : 213, 214, 215, 216, 217, 219, 220, 221, 222, 223, 225, 226, 227, 228, 230,
3608 : 231, 232, 234, 235, 236, 238, 239, 240, 242, 243, 245, 246, 248, 250, 251,
3609 : 253};
3610 :
3611 : static int rtc_minq_12[QINDEX_RANGE] = {
3612 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3613 : 0, 0, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22, 22,
3614 : 23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 33,
3615 : 34, 34, 35, 36, 37, 37, 38, 39, 39, 40, 41, 41, 42, 43, 43,
3616 : 44, 45, 45, 46, 46, 47, 48, 48, 49, 50, 50, 51, 52, 52, 53,
3617 : 54, 54, 55, 55, 56, 57, 57, 58, 58, 59, 60, 60, 61, 62, 62,
3618 : 63, 63, 64, 65, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76,
3619 : 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94,
3620 : 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 108,
3621 : 109, 110, 111, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
3622 : 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
3623 : 139, 140, 141, 142, 143, 144, 145, 146, 146, 147, 148, 149, 150, 151, 152,
3624 : 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 163, 164, 165, 166,
3625 : 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
3626 : 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
3627 : 197, 199, 200, 201, 202, 203, 205, 206, 207, 208, 210, 211, 212, 214, 215,
3628 : 216, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 234, 235, 237, 239,
3629 : 241};
3630 : */
3631 :
3632 : static int gf_high = 2000;
3633 : static int gf_low = 400;
3634 : static int kf_high = 5000;
3635 : static int kf_low = 400;
3636 :
3637 104 : static int get_active_quality(int q, int gfu_boost, int low, int high,
3638 : int *low_motion_minq, int *high_motion_minq) {
3639 104 : if (gfu_boost > high)
3640 0 : return low_motion_minq[q];
3641 104 : else if (gfu_boost < low)
3642 0 : return high_motion_minq[q];
3643 : else {
3644 104 : const int gap = high - low;
3645 104 : const int offset = high - gfu_boost;
3646 104 : const int qdiff = high_motion_minq[q] - low_motion_minq[q];
3647 104 : const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
3648 104 : return low_motion_minq[q] + adjustment;
3649 : }
3650 : }
3651 :
3652 100 : static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
3653 : AomBitDepth bit_depth) {
3654 : int *kf_low_motion_minq;
3655 : int *kf_high_motion_minq;
3656 100 : ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
3657 100 : ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
3658 100 : return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
3659 : kf_low_motion_minq, kf_high_motion_minq);
3660 : }
3661 :
3662 4 : static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
3663 : AomBitDepth bit_depth) {
3664 : int *arfgf_low_motion_minq;
3665 : int *arfgf_high_motion_minq;
3666 4 : ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
3667 4 : ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
3668 4 : return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
3669 : arfgf_low_motion_minq, arfgf_high_motion_minq);
3670 : }
3671 :
3672 4 : static int get_gf_high_motion_quality(int q, AomBitDepth bit_depth) {
3673 : int *arfgf_high_motion_minq;
3674 4 : ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
3675 4 : return arfgf_high_motion_minq[q];
3676 : }
3677 : #if TWO_PASS
3678 : /******************************************************
3679 : * adaptive_qindex_calc_two_pass
3680 : * assigns the q_index per frame using average reference area per frame.
3681 : * used in the second pass of two pass encoding
3682 : ******************************************************/
3683 0 : static int adaptive_qindex_calc_two_pass(
3684 : PictureControlSet *picture_control_set_ptr,
3685 : RATE_CONTROL *rc,
3686 : int qindex) {
3687 :
3688 0 : SequenceControlSet *sequence_control_set_ptr = picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr;
3689 : #if !TWO_PASS_IMPROVEMENT
3690 : const Av1Common *const cm = picture_control_set_ptr->parent_pcs_ptr->av1_cm;
3691 : #endif
3692 0 : const int cq_level = qindex;
3693 0 : int active_best_quality = 0;
3694 0 : int active_worst_quality = qindex;
3695 0 : rc->arf_q = 0;
3696 : int q;
3697 : int is_src_frame_alt_ref, refresh_golden_frame, refresh_alt_ref_frame, is_intrl_arf_boost, rf_level, update_type;
3698 0 : is_src_frame_alt_ref = 0;
3699 0 : refresh_golden_frame = frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr) ? 1 : 0;
3700 0 : refresh_alt_ref_frame = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? 1 : 0;
3701 0 : is_intrl_arf_boost = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index > 0 && picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag) ? 1 : 0;
3702 0 : rf_level = (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) ? KF_STD :
3703 0 : (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? GF_ARF_STD :
3704 0 : picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ? GF_ARF_LOW : INTER_NORMAL;
3705 :
3706 0 : update_type = (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) ? KF_UPDATE :
3707 0 : (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? ARF_UPDATE :
3708 0 : picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ? INTNL_ARF_UPDATE : LF_UPDATE;
3709 0 : const int bit_depth = sequence_control_set_ptr->static_config.encoder_bit_depth;
3710 : // 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.
3711 : // So, for each frame, arf_q is updated based on the qp of its references.
3712 0 : rc->arf_q = MAX(rc->arf_q, ((picture_control_set_ptr->ref_pic_qp_array[0][0] << 2) + 2));
3713 0 : if (picture_control_set_ptr->slice_type == B_SLICE)
3714 0 : rc->arf_q = MAX(rc->arf_q, ((picture_control_set_ptr->ref_pic_qp_array[1][0] << 2) + 2));
3715 0 : uint64_t referenced_area_avg = picture_control_set_ptr->parent_pcs_ptr->referenced_area_avg;
3716 0 : uint64_t referenced_area_max = 64;
3717 :
3718 0 : if (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) {
3719 : // Not forced keyframe.
3720 0 : double q_adj_factor = 1.0;
3721 : double q_val;
3722 0 : rc->worst_quality = MAXQ;
3723 0 : rc->best_quality = MINQ;
3724 0 : referenced_area_max = MAX_REF_AREA_I;
3725 :
3726 0 : if (referenced_area_avg <= 16)
3727 0 : referenced_area_avg = 0;
3728 : // cross multiplication to derive kf_boost from referenced area; kf_boost range is [kf_low,kf_high], and referenced range [0,referenced_area_max]
3729 0 : rc->kf_boost = (int)((referenced_area_avg * (kf_high - kf_low)) / referenced_area_max) + kf_low;
3730 : // Baseline value derived from cpi->active_worst_quality and kf boost.
3731 : active_best_quality =
3732 0 : get_kf_active_quality(rc, active_worst_quality, bit_depth);
3733 : #if !TWO_PASS_IMPROVEMENT
3734 : // Allow somewhat lower kf minq with small image formats.
3735 : if ((cm->frm_size.frame_width * cm->frm_size.frame_height) <= (352 * 288))
3736 : q_adj_factor -= 0.25;
3737 : #endif
3738 : // Make a further adjustment based on the kf zero motion measure.
3739 0 : q_adj_factor += 0.05 - (0.001 * (double)picture_control_set_ptr->parent_pcs_ptr->kf_zeromotion_pct/*(double)cpi->twopass.kf_zeromotion_pct*/);
3740 :
3741 : // Convert the adjustment factor to a qindex delta
3742 : // on active_best_quality.
3743 0 : q_val = eb_av1_convert_qindex_to_q(active_best_quality, bit_depth);
3744 0 : active_best_quality +=
3745 0 : eb_av1_compute_qdelta(q_val, q_val * q_adj_factor, bit_depth);
3746 : }
3747 0 : else if (!is_src_frame_alt_ref &&
3748 0 : (refresh_golden_frame || is_intrl_arf_boost ||
3749 : refresh_alt_ref_frame)) {
3750 :
3751 0 : referenced_area_max = sequence_control_set_ptr->input_resolution < 2 ? MAX_REF_AREA_NONI_LOW_RES :
3752 0 : ((int)referenced_area_avg - (int)picture_control_set_ptr->ref_pic_referenced_area_avg_array[0][0] >= REF_AREA_DIF_THRESHOLD) ?
3753 : MAX_REF_AREA_NONI_LOW_RES : MAX_REF_AREA_NONI;
3754 :
3755 : // Clip the complexity of highly complex pictures to maximum.
3756 0 : if (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity > HIGH_QPS_COMP_THRESHOLD)
3757 0 : referenced_area_avg = 0;
3758 :
3759 0 : rc->arf_boost_factor =
3760 0 : ((int)referenced_area_avg - (int)picture_control_set_ptr->ref_pic_referenced_area_avg_array[0][0] >= REF_AREA_DIF_THRESHOLD
3761 0 : && referenced_area_avg > 20 && picture_control_set_ptr->ref_pic_referenced_area_avg_array[0][0] <= 20) ? (float_t)1.3 : (float_t)1;
3762 0 : rc->gfu_boost = (int)(((referenced_area_avg) * (gf_high - gf_low)) / referenced_area_max) + gf_low;
3763 0 : q = active_worst_quality;
3764 :
3765 : // non ref frame or repeated frames with re-encode
3766 0 : if (!refresh_alt_ref_frame && !is_intrl_arf_boost)
3767 0 : active_best_quality = cq_level;
3768 : else {
3769 : // base layer
3770 0 : if (update_type == ARF_UPDATE) {
3771 0 : active_best_quality = get_gf_active_quality(rc, q, bit_depth);
3772 0 : rc->arf_q = active_best_quality;
3773 0 : const int min_boost = get_gf_high_motion_quality(q, bit_depth);
3774 0 : const int boost = min_boost - active_best_quality;
3775 :
3776 0 : active_best_quality = min_boost - (int)(boost * rc->arf_boost_factor);
3777 0 : if (picture_control_set_ptr->parent_pcs_ptr->sad_me / picture_control_set_ptr->sb_total_count / 256 < ME_SAD_LOW_THRESHOLD1)
3778 0 : active_best_quality = active_best_quality * 130 / 100;
3779 0 : else if (picture_control_set_ptr->parent_pcs_ptr->sad_me / picture_control_set_ptr->sb_total_count / 256 < ME_SAD_LOW_THRESHOLD2)
3780 0 : active_best_quality = active_best_quality * 115 / 100;
3781 : }
3782 : else
3783 0 : active_best_quality = rc->arf_q;
3784 : // active_best_quality is updated with the q index of the reference
3785 0 : if (rf_level == GF_ARF_LOW)
3786 0 : active_best_quality = (active_best_quality + cq_level + 1) / 2;
3787 : }
3788 : }
3789 : else
3790 0 : active_best_quality = cq_level;
3791 0 : q = active_best_quality;
3792 0 : clamp(q, active_best_quality, active_worst_quality);
3793 :
3794 0 : return q;
3795 : }
3796 : #endif
3797 106 : static int adaptive_qindex_calc(
3798 : PictureControlSet *picture_control_set_ptr,
3799 : RATE_CONTROL *rc,
3800 : int qindex) {
3801 106 : SequenceControlSet *sequence_control_set_ptr = picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr;
3802 106 : const Av1Common *const cm = picture_control_set_ptr->parent_pcs_ptr->av1_cm;
3803 :
3804 106 : const int cq_level = qindex;
3805 106 : int active_best_quality = 0;
3806 106 : int active_worst_quality = qindex;
3807 106 : rc->arf_q = 0;
3808 : int q;
3809 : int is_src_frame_alt_ref, refresh_golden_frame, refresh_alt_ref_frame, is_intrl_arf_boost, rf_level, update_type;
3810 106 : is_src_frame_alt_ref = 0;
3811 106 : refresh_golden_frame = frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr) ? 1 : 0;
3812 106 : refresh_alt_ref_frame = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? 1 : 0;
3813 106 : is_intrl_arf_boost = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index > 0 && picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag) ? 1 : 0;
3814 208 : rf_level = (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) ? KF_STD :
3815 200 : (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? GF_ARF_STD :
3816 98 : picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ? GF_ARF_LOW : INTER_NORMAL;
3817 :
3818 208 : update_type = (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) ? KF_UPDATE :
3819 200 : (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? ARF_UPDATE :
3820 98 : picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ? INTNL_ARF_UPDATE : LF_UPDATE;
3821 106 : const int bit_depth = sequence_control_set_ptr->static_config.encoder_bit_depth;
3822 : // 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.
3823 : // So, for each frame, arf_q is updated based on the qp of its references.
3824 106 : if (picture_control_set_ptr->ref_slice_type_array[0][0] != I_SLICE)
3825 86 : rc->arf_q = MAX(rc->arf_q, ((picture_control_set_ptr->ref_pic_qp_array[0][0] << 2) + 2));
3826 106 : if ((picture_control_set_ptr->slice_type == B_SLICE) && (picture_control_set_ptr->ref_slice_type_array[1][0] != I_SLICE))
3827 90 : rc->arf_q = MAX(rc->arf_q, ((picture_control_set_ptr->ref_pic_qp_array[1][0] << 2) + 2));
3828 106 : if (frame_is_intra_only(picture_control_set_ptr->parent_pcs_ptr)) {
3829 : // Not forced keyframe.
3830 4 : double q_adj_factor = 1.0;
3831 : double q_val;
3832 :
3833 4 : rc->worst_quality = MAXQ;
3834 4 : rc->best_quality = MINQ;
3835 4 : int max_qp_scaling_avg_comp_I = sequence_control_set_ptr->input_resolution < 2 ? MAX_QPS_COMP_I_LR :
3836 0 : picture_control_set_ptr->parent_pcs_ptr->sc_content_detected ? (MAX_QPS_COMP_I >> 1) : (MAX_QPS_COMP_I);
3837 :
3838 : // Clip the complexity of highly complex pictures to maximum.
3839 4 : if (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity > HIGH_QPS_COMP_THRESHOLD)
3840 0 : picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity = max_qp_scaling_avg_comp_I;
3841 : // For the low filtered ALT_REF pictures (next ALT_REF) where complexity is low and picture is static, decrease the complexity/QP of the I_SLICE.
3842 : // The improved area will be propagated to future frames
3843 4 : if (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity <= LOW_QPS_COMP_THRESHOLD &&
3844 2 : picture_control_set_ptr->parent_pcs_ptr->filtered_sse < LOW_FILTERED_THRESHOLD && picture_control_set_ptr->parent_pcs_ptr->filtered_sse_uv < LOW_FILTERED_THRESHOLD &&
3845 2 : picture_control_set_ptr->parent_pcs_ptr->kf_zeromotion_pct > STATIC_KF_GROUP_THRESH)
3846 0 : picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity >>= 1;
3847 : // For the highly filtered ALT_REF pictures (next ALT_REF), increase the complexity/QP of the I_SLICE to save on rate
3848 4 : if (picture_control_set_ptr->parent_pcs_ptr->filtered_sse + picture_control_set_ptr->parent_pcs_ptr->filtered_sse_uv >= (7 << 8))
3849 0 : picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity = max_qp_scaling_avg_comp_I;
3850 4 : picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity = MIN(max_qp_scaling_avg_comp_I, picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity);
3851 :
3852 : // cross multiplication to derive kf_boost from non_moving_average_score; kf_boost range is [kf_low,kf_high], and non_moving_average_score range [0,max_qp_scaling_avg_comp_I]
3853 4 : rc->kf_boost = (((max_qp_scaling_avg_comp_I - (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity)) * (kf_high - kf_low)) / max_qp_scaling_avg_comp_I) + kf_low;
3854 : // Baseline value derived from cpi->active_worst_quality and kf boost.
3855 : active_best_quality =
3856 4 : get_kf_active_quality(rc, active_worst_quality, bit_depth);
3857 : // Allow somewhat lower kf minq with small image formats.
3858 4 : if ((cm->frm_size.frame_width * cm->frm_size.frame_height) <= (352 * 288))
3859 0 : q_adj_factor -= 0.25;
3860 : // Make a further adjustment based on the kf zero motion measure.
3861 4 : q_adj_factor += 0.05 - (0.001 * (double)picture_control_set_ptr->parent_pcs_ptr->kf_zeromotion_pct/*(double)cpi->twopass.kf_zeromotion_pct*/);
3862 :
3863 : // Convert the adjustment factor to a qindex delta
3864 : // on active_best_quality.
3865 4 : q_val = eb_av1_convert_qindex_to_q(active_best_quality, bit_depth);
3866 4 : active_best_quality +=
3867 4 : eb_av1_compute_qdelta(q_val, q_val * q_adj_factor, bit_depth);
3868 : }
3869 102 : else if (!is_src_frame_alt_ref &&
3870 102 : (refresh_golden_frame || is_intrl_arf_boost ||
3871 : refresh_alt_ref_frame)) {
3872 : // Clip the complexity of highly complex pictures to maximum.
3873 57 : if (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity > HIGH_QPS_COMP_THRESHOLD)
3874 0 : picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity = MAX_QPS_COMP_NONI;
3875 57 : rc->gfu_boost = (((MAX_QPS_COMP_NONI - (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity)) * (gf_high - gf_low)) / MAX_QPS_COMP_NONI) + gf_low;
3876 : // For the highly filtered ALT_REF pictures or where complexity is medium or picture is medium moving, add a boost to decrease the QP of the ALT_REF.
3877 : // The improved area will be propagated to future frames
3878 29 : rc->arf_boost_factor = (picture_control_set_ptr->parent_pcs_ptr->qp_scaling_average_complexity > LOW_QPS_COMP_THRESHOLD || picture_control_set_ptr->parent_pcs_ptr->kf_zeromotion_pct < MEDIUM_MOVING_KF_GROUP_THRESH || picture_control_set_ptr->parent_pcs_ptr->filtered_sse >= HIGH_FILTERED_THRESHOLD) ?
3879 86 : (float_t)1.3 : 1;
3880 57 : q = active_worst_quality;
3881 :
3882 : // non ref frame or repeated frames with re-encode
3883 57 : if (!refresh_alt_ref_frame && !is_intrl_arf_boost)
3884 0 : active_best_quality = cq_level;
3885 : else {
3886 : // base layer
3887 57 : if (update_type == ARF_UPDATE) {
3888 4 : active_best_quality = get_gf_active_quality(rc, q, bit_depth);
3889 : //*arf_q = active_best_quality;
3890 4 : rc->arf_q = active_best_quality;
3891 4 : const int min_boost = get_gf_high_motion_quality(q, bit_depth);
3892 4 : const int boost = min_boost - active_best_quality;
3893 :
3894 4 : active_best_quality = min_boost - (int)(boost * rc->arf_boost_factor);
3895 : }
3896 : else
3897 53 : active_best_quality = rc->arf_q;
3898 : // active_best_quality is updated with the q index of the reference
3899 57 : if (rf_level == GF_ARF_LOW)
3900 53 : active_best_quality = (active_best_quality + cq_level + 1) / 2;
3901 : }
3902 : }
3903 : else
3904 45 : active_best_quality = cq_level;
3905 106 : q = active_best_quality;
3906 106 : clamp(q, active_best_quality, active_worst_quality);
3907 :
3908 106 : return q;
3909 : }
3910 : #if TWO_PASS
3911 : /******************************************************
3912 : * sb_qp_derivation_two_pass
3913 : * Calculates the QP per SB based on the referenced area
3914 : * used in the second pass of two pass encoding
3915 : ******************************************************/
3916 0 : static void sb_qp_derivation_two_pass(
3917 : PictureControlSet *picture_control_set_ptr) {
3918 :
3919 0 : SequenceControlSet *sequence_control_set_ptr = picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr;
3920 : LargestCodingUnit *sb_ptr;
3921 : uint32_t sb_addr;
3922 :
3923 0 : picture_control_set_ptr->parent_pcs_ptr->average_qp = 0;
3924 0 : if (sequence_control_set_ptr->use_input_stat_file && picture_control_set_ptr->temporal_layer_index <= 0)
3925 0 : picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 1;
3926 : else
3927 0 : picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 0;
3928 :
3929 0 : if (picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present) {
3930 0 : const int bit_depth = sequence_control_set_ptr->static_config.encoder_bit_depth;
3931 0 : int active_worst_quality = quantizer_to_qindex[(uint8_t)sequence_control_set_ptr->qp];
3932 : int *kf_low_motion_minq;
3933 : int *kf_high_motion_minq;
3934 0 : ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
3935 0 : ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
3936 :
3937 0 : uint32_t me_sb_size = sequence_control_set_ptr->sb_sz;
3938 0 : uint32_t me_pic_width_in_sb = (sequence_control_set_ptr->seq_header.max_frame_width + sequence_control_set_ptr->sb_sz - 1) / me_sb_size;
3939 0 : uint32_t me_pic_height_in_sb = (sequence_control_set_ptr->seq_header.max_frame_height + me_sb_size - 1) / me_sb_size;
3940 :
3941 : int *arfgf_low_motion_minq;
3942 : int *arfgf_high_motion_minq;
3943 0 : ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
3944 0 : ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
3945 :
3946 0 : int max_delta_qp = (picture_control_set_ptr->slice_type == 2) ?
3947 0 : ((kf_high_motion_minq[active_worst_quality] - kf_low_motion_minq[active_worst_quality] + 2) >> 2) / 2 :
3948 0 : ((arfgf_high_motion_minq[active_worst_quality] - arfgf_low_motion_minq[active_worst_quality] + 2) >> 2) / 2;
3949 :
3950 0 : for (sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_tot_cnt; ++sb_addr) {
3951 0 : sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_addr];
3952 0 : int delta_qp = 0;
3953 : uint16_t variance_sb;
3954 : uint32_t referenced_area_sb, me_distortion;
3955 :
3956 0 : if (sequence_control_set_ptr->seq_header.sb_size == BLOCK_128X128) {
3957 0 : uint32_t me_sb_x = (sb_ptr->origin_x / me_sb_size);
3958 0 : uint32_t me_sb_y = (sb_ptr->origin_y / me_sb_size);
3959 0 : uint32_t me_sb_addr_0 = me_sb_x + me_sb_y * me_pic_width_in_sb;
3960 0 : uint32_t me_sb_addr_1 = (me_sb_x + 1) < me_pic_width_in_sb ? (me_sb_x + 1) + ((me_sb_y + 0) * me_pic_width_in_sb) : me_sb_addr_0;
3961 0 : uint32_t me_sb_addr_2 = (me_sb_y + 1) < me_pic_height_in_sb ? (me_sb_x + 0) + ((me_sb_y + 1) * me_pic_width_in_sb) : me_sb_addr_0;
3962 0 : uint32_t me_sb_addr_3 = ((me_sb_x + 1) < me_pic_width_in_sb) && ((me_sb_y + 1) < me_pic_height_in_sb) ?
3963 0 : (me_sb_x + 1) + ((me_sb_y + 1) * me_pic_width_in_sb) : me_sb_addr_0;
3964 :
3965 0 : variance_sb =
3966 0 : (picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_0][ME_TIER_ZERO_PU_64x64] +
3967 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_1][ME_TIER_ZERO_PU_64x64] +
3968 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_2][ME_TIER_ZERO_PU_64x64] +
3969 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_3][ME_TIER_ZERO_PU_64x64] + 2) >> 2;
3970 :
3971 0 : referenced_area_sb =
3972 0 : (picture_control_set_ptr->parent_pcs_ptr->stat_struct.referenced_area[me_sb_addr_0]
3973 0 : / sequence_control_set_ptr->sb_params_array[me_sb_addr_0].width / sequence_control_set_ptr->sb_params_array[me_sb_addr_0].height +
3974 0 : picture_control_set_ptr->parent_pcs_ptr->stat_struct.referenced_area[me_sb_addr_1]
3975 0 : / sequence_control_set_ptr->sb_params_array[me_sb_addr_1].width / sequence_control_set_ptr->sb_params_array[me_sb_addr_1].height +
3976 0 : picture_control_set_ptr->parent_pcs_ptr->stat_struct.referenced_area[me_sb_addr_2]
3977 0 : / sequence_control_set_ptr->sb_params_array[me_sb_addr_2].width / sequence_control_set_ptr->sb_params_array[me_sb_addr_2].height +
3978 0 : picture_control_set_ptr->parent_pcs_ptr->stat_struct.referenced_area[me_sb_addr_3]
3979 0 : / sequence_control_set_ptr->sb_params_array[me_sb_addr_3].width / sequence_control_set_ptr->sb_params_array[me_sb_addr_3].height + 2) >> 2;
3980 0 : me_distortion =
3981 0 : (picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[me_sb_addr_0] +
3982 0 : picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[me_sb_addr_1] +
3983 0 : picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[me_sb_addr_2] +
3984 0 : picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[me_sb_addr_3] + 2) >> 2;
3985 0 : me_distortion >>= 8;
3986 : }
3987 : else {
3988 0 : variance_sb = picture_control_set_ptr->parent_pcs_ptr->variance[sb_addr][ME_TIER_ZERO_PU_64x64];
3989 0 : referenced_area_sb = picture_control_set_ptr->parent_pcs_ptr->stat_struct.referenced_area[sb_addr]
3990 0 : / sequence_control_set_ptr->sb_params_array[sb_addr].width / sequence_control_set_ptr->sb_params_array[sb_addr].height;
3991 0 : me_distortion = picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_addr] >> 8;
3992 : }
3993 0 : delta_qp = 0;
3994 :
3995 0 : if (picture_control_set_ptr->slice_type == 2) {
3996 0 : referenced_area_sb = MIN(REF_AREA_MED_THRESHOLD + REF_AREA_LOW_THRESHOLD, referenced_area_sb);
3997 0 : if (referenced_area_sb >= REF_AREA_MED_THRESHOLD)
3998 0 : delta_qp = -(max_delta_qp * ((int)referenced_area_sb - REF_AREA_MED_THRESHOLD) / (REF_AREA_MED_THRESHOLD));
3999 : else
4000 0 : delta_qp = max_delta_qp;
4001 :
4002 0 : if (delta_qp < 0 && variance_sb < IS_COMPLEX_LCU_FLAT_VARIANCE_TH)
4003 0 : delta_qp = 0;
4004 : }
4005 0 : else if (picture_control_set_ptr->temporal_layer_index == 0) {
4006 0 : if (referenced_area_sb < REF_AREA_LOW_THRESHOLD)
4007 0 : delta_qp = max_delta_qp >> 1;
4008 : #if !TWO_PASS_IMPROVEMENT
4009 : else if (referenced_area_sb > MAX_REF_AREA_NONI_LOW_RES && me_distortion > ME_SAD_HIGH_THRESHOLD)
4010 : delta_qp = -max_delta_qp >> 2;
4011 : #endif
4012 : }
4013 :
4014 0 : if (picture_control_set_ptr->slice_type == 2)
4015 0 : sb_ptr->qp = CLIP3(
4016 : MIN(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((kf_low_motion_minq[active_worst_quality] + 2) >> 2)),
4017 : MAX(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((kf_high_motion_minq[active_worst_quality] + 2) >> 2)) + 3,
4018 : ((int16_t)picture_control_set_ptr->parent_pcs_ptr->picture_qp + (int16_t)delta_qp));
4019 : else
4020 0 : sb_ptr->qp = CLIP3(
4021 : MIN(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((arfgf_low_motion_minq[active_worst_quality] + 2) >> 2)) - 1,
4022 : MAX(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((arfgf_high_motion_minq[active_worst_quality] + 2) >> 2)) + 3,
4023 : ((int16_t)picture_control_set_ptr->parent_pcs_ptr->picture_qp + (int16_t)delta_qp));
4024 :
4025 0 : sb_ptr->qp = CLIP3(
4026 : sequence_control_set_ptr->static_config.min_qp_allowed,
4027 : sequence_control_set_ptr->static_config.max_qp_allowed,
4028 : sb_ptr->qp);
4029 0 : sb_ptr->delta_qp = (int)picture_control_set_ptr->parent_pcs_ptr->picture_qp - (int)sb_ptr->qp;
4030 0 : picture_control_set_ptr->parent_pcs_ptr->average_qp += sb_ptr->qp;
4031 : }
4032 : }
4033 : else {
4034 0 : for (sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_tot_cnt; ++sb_addr) {
4035 0 : sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_addr];
4036 0 : sb_ptr->qp = (uint8_t)picture_control_set_ptr->picture_qp;
4037 0 : sb_ptr->delta_qp = 0;
4038 0 : picture_control_set_ptr->parent_pcs_ptr->average_qp += sb_ptr->qp;
4039 : }
4040 : }
4041 0 : }
4042 : #endif
4043 : // Calculates the QP per SB based on the non moving index. For now, only active for I Slice.
4044 106 : static void sb_qp_derivation(
4045 : PictureControlSet *picture_control_set_ptr) {
4046 :
4047 106 : SequenceControlSet *sequence_control_set_ptr = picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr;
4048 : LargestCodingUnit *sb_ptr;
4049 : uint32_t sb_addr;
4050 : RATE_CONTROL rc;
4051 106 : picture_control_set_ptr->parent_pcs_ptr->average_qp = 0;
4052 : #if TWO_PASS
4053 106 : if (picture_control_set_ptr->slice_type == 2)
4054 : #else
4055 : if (sequence_control_set_ptr->static_config.enable_adaptive_quantization == 2 && picture_control_set_ptr->slice_type == 2 &&
4056 : picture_control_set_ptr->parent_pcs_ptr->frames_in_sw >= QPS_SW_THRESH && !picture_control_set_ptr->parent_pcs_ptr->sc_content_detected)
4057 : #endif
4058 4 : picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 1;
4059 : else
4060 102 : picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 0;
4061 :
4062 106 : if (picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present) {
4063 4 : const int bit_depth = sequence_control_set_ptr->static_config.encoder_bit_depth;
4064 4 : int active_best_quality = 0;
4065 4 : int active_worst_quality = quantizer_to_qindex[(uint8_t)sequence_control_set_ptr->qp];
4066 : int *kf_low_motion_minq;
4067 : int *kf_high_motion_minq;
4068 4 : ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
4069 4 : ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
4070 : double q_val, picture_q_val;
4071 4 : uint32_t me_sb_size = sequence_control_set_ptr->sb_sz;
4072 4 : uint32_t me_pic_width_in_sb = (sequence_control_set_ptr->seq_header.max_frame_width + sequence_control_set_ptr->sb_sz - 1) / me_sb_size;
4073 4 : uint32_t me_pic_height_in_sb = (sequence_control_set_ptr->seq_header.max_frame_height + me_sb_size - 1) / me_sb_size;
4074 4 : int max_qp_scaling_avg_comp =
4075 4 : MAX(1, picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance + picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance);
4076 : // Calculate the QP per frames
4077 4 : rc.kf_boost = (((max_qp_scaling_avg_comp - picture_control_set_ptr->parent_pcs_ptr->non_moving_index_average) * (kf_high - kf_low))
4078 4 : / max_qp_scaling_avg_comp) + kf_low;
4079 : active_best_quality =
4080 4 : get_kf_active_quality(&rc, active_worst_quality, bit_depth);
4081 : // Convert the adjustment factor to a qindex delta
4082 : // on active_best_quality.
4083 4 : picture_q_val = eb_av1_convert_qindex_to_q(active_best_quality, bit_depth);
4084 244 : for (sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_tot_cnt; ++sb_addr) {
4085 240 : sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_addr];
4086 240 : int delta_qp = 0;
4087 240 : SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_addr];
4088 : uint8_t non_moving_index_sb;
4089 : uint16_t variance_sb;
4090 240 : if (sequence_control_set_ptr->seq_header.sb_size == BLOCK_128X128) {
4091 0 : uint32_t me_sb_x = (sb_ptr->origin_x / me_sb_size);
4092 0 : uint32_t me_sb_y = (sb_ptr->origin_y / me_sb_size);
4093 0 : uint32_t me_sb_addr_0 = me_sb_x + me_sb_y * me_pic_width_in_sb;
4094 0 : uint32_t me_sb_addr_1 = (me_sb_x + 1) < me_pic_width_in_sb ? (me_sb_x + 1) + ((me_sb_y + 0) * me_pic_width_in_sb) : me_sb_addr_0;
4095 0 : uint32_t me_sb_addr_2 = (me_sb_y + 1) < me_pic_height_in_sb ? (me_sb_x + 0) + ((me_sb_y + 1) * me_pic_width_in_sb) : me_sb_addr_0;
4096 0 : uint32_t me_sb_addr_3 = ((me_sb_x + 1) < me_pic_width_in_sb) && ((me_sb_y + 1) < me_pic_height_in_sb) ?
4097 0 : (me_sb_x + 1) + ((me_sb_y + 1) * me_pic_width_in_sb) : me_sb_addr_0;
4098 0 : non_moving_index_sb =
4099 0 : (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[me_sb_addr_0] +
4100 0 : picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[me_sb_addr_1] +
4101 0 : picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[me_sb_addr_2] +
4102 0 : picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[me_sb_addr_3] + 2) >> 2;
4103 0 : variance_sb =
4104 0 : (picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_0][ME_TIER_ZERO_PU_64x64] +
4105 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_1][ME_TIER_ZERO_PU_64x64] +
4106 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_2][ME_TIER_ZERO_PU_64x64] +
4107 0 : picture_control_set_ptr->parent_pcs_ptr->variance[me_sb_addr_3][ME_TIER_ZERO_PU_64x64] + 2) >> 2;
4108 : }
4109 : else {
4110 240 : non_moving_index_sb = picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_addr];
4111 240 : variance_sb = picture_control_set_ptr->parent_pcs_ptr->variance[sb_addr][ME_TIER_ZERO_PU_64x64];
4112 : }
4113 240 : if (sb_params->is_complete_sb && max_qp_scaling_avg_comp >= 10 && picture_control_set_ptr->parent_pcs_ptr->non_moving_index_average < 20 &&
4114 200 : non_moving_index_sb < picture_control_set_ptr->parent_pcs_ptr->non_moving_index_average) {
4115 92 : if (variance_sb < IS_COMPLEX_LCU_FLAT_VARIANCE_TH)
4116 0 : delta_qp = 3;
4117 : else {
4118 : // Calculate the QP of each block to find the delta
4119 92 : rc.kf_boost = (((max_qp_scaling_avg_comp - non_moving_index_sb) * (kf_high - kf_low)) / max_qp_scaling_avg_comp) + kf_low;
4120 : // Baseline value derived from cpi->active_worst_quality and kf boost.
4121 : active_best_quality =
4122 92 : get_kf_active_quality(&rc, active_worst_quality, bit_depth);
4123 : // Convert the adjustment factor to a qindex delta
4124 : // on active_best_quality.
4125 92 : q_val = eb_av1_convert_qindex_to_q(active_best_quality, bit_depth);
4126 92 : delta_qp = (int16_t)q_val - (int16_t)picture_q_val;
4127 : }
4128 : }
4129 240 : sb_ptr->qp = CLIP3(
4130 : MIN(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((kf_low_motion_minq[active_worst_quality] + 2) >> 2)),
4131 : MAX(picture_control_set_ptr->parent_pcs_ptr->picture_qp, ((kf_high_motion_minq[active_worst_quality] + 2) >> 2)) + 3,
4132 : ((int16_t)picture_control_set_ptr->parent_pcs_ptr->picture_qp + (int16_t)delta_qp));
4133 240 : sb_ptr->qp = CLIP3(
4134 : sequence_control_set_ptr->static_config.min_qp_allowed,
4135 : sequence_control_set_ptr->static_config.max_qp_allowed,
4136 : sb_ptr->qp);
4137 240 : sb_ptr->delta_qp = (int)picture_control_set_ptr->parent_pcs_ptr->picture_qp - (int)sb_ptr->qp;
4138 240 : picture_control_set_ptr->parent_pcs_ptr->average_qp += sb_ptr->qp;
4139 : }
4140 : }
4141 : else {
4142 6222 : for (sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_tot_cnt; ++sb_addr) {
4143 6120 : sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_addr];
4144 6120 : sb_ptr->qp = (uint8_t)picture_control_set_ptr->picture_qp;
4145 6120 : sb_ptr->delta_qp = 0;
4146 6120 : picture_control_set_ptr->parent_pcs_ptr->average_qp += sb_ptr->qp;
4147 : }
4148 : }
4149 106 : }
4150 2 : void* rate_control_kernel(void *input_ptr)
4151 : {
4152 : // Context
4153 2 : RateControlContext *context_ptr = (RateControlContext *)input_ptr;
4154 :
4155 : RateControlIntervalParamContext *rate_control_param_ptr;
4156 :
4157 : RateControlIntervalParamContext *prev_gop_rate_control_param_ptr;
4158 : RateControlIntervalParamContext *next_gop_rate_control_param_ptr;
4159 :
4160 : PictureControlSet *picture_control_set_ptr;
4161 : PictureParentControlSet *parentpicture_control_set_ptr;
4162 :
4163 : // Config
4164 : SequenceControlSet *sequence_control_set_ptr;
4165 :
4166 : // Input
4167 : EbObjectWrapper *rate_control_tasks_wrapper_ptr;
4168 : RateControlTasks *rate_control_tasks_ptr;
4169 :
4170 : // Output
4171 : EbObjectWrapper *rate_control_results_wrapper_ptr;
4172 : RateControlResults *rate_control_results_ptr;
4173 :
4174 : RateControlLayerContext *rate_control_layer_ptr;
4175 :
4176 2 : uint64_t total_number_of_fb_frames = 0;
4177 :
4178 : RateControlTaskTypes task_type;
4179 : EbRateControlModel *rc_model_ptr;
4180 : RATE_CONTROL rc;
4181 :
4182 2 : rc_model_ptr = context_ptr->rc_model_ptr;
4183 :
4184 : for (;;) {
4185 : // Get RateControl Task
4186 962 : eb_get_full_object(
4187 : context_ptr->rate_control_input_tasks_fifo_ptr,
4188 : &rate_control_tasks_wrapper_ptr);
4189 :
4190 960 : rate_control_tasks_ptr = (RateControlTasks*)rate_control_tasks_wrapper_ptr->object_ptr;
4191 960 : task_type = rate_control_tasks_ptr->task_type;
4192 :
4193 : // Modify these for different temporal layers later
4194 960 : switch (task_type) {
4195 120 : case RC_PICTURE_MANAGER_RESULT:
4196 :
4197 120 : picture_control_set_ptr = (PictureControlSet *)rate_control_tasks_ptr->picture_control_set_wrapper_ptr->object_ptr;
4198 120 : sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
4199 120 : FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
4200 :
4201 120 : if (picture_control_set_ptr->picture_number == 0) {
4202 2 : rate_control_model_init(rc_model_ptr, sequence_control_set_ptr);
4203 :
4204 : //init rate control parameters
4205 2 : init_rc(
4206 : context_ptr,
4207 : picture_control_set_ptr,
4208 : sequence_control_set_ptr);
4209 : }
4210 : #if TWO_PASS
4211 : // SB Loop
4212 120 : picture_control_set_ptr->parent_pcs_ptr->sad_me = 0;
4213 120 : if (picture_control_set_ptr->slice_type != 2)
4214 7076 : for (int sb_addr = 0; sb_addr < picture_control_set_ptr->sb_total_count; ++sb_addr) {
4215 6960 : picture_control_set_ptr->parent_pcs_ptr->sad_me += picture_control_set_ptr->parent_pcs_ptr->rc_me_distortion[sb_addr];
4216 : }
4217 : #endif
4218 120 : if (sequence_control_set_ptr->static_config.rate_control_mode)
4219 : {
4220 0 : picture_control_set_ptr->parent_pcs_ptr->intra_selected_org_qp = 0;
4221 : // High level RC
4222 0 : if (sequence_control_set_ptr->static_config.rate_control_mode == 2)
4223 0 : high_level_rc_input_picture_vbr(
4224 0 : picture_control_set_ptr->parent_pcs_ptr,
4225 : sequence_control_set_ptr,
4226 : sequence_control_set_ptr->encode_context_ptr,
4227 : context_ptr,
4228 : context_ptr->high_level_rate_control_ptr);
4229 0 : else if (sequence_control_set_ptr->static_config.rate_control_mode == 3)
4230 0 : high_level_rc_input_picture_cvbr(
4231 0 : picture_control_set_ptr->parent_pcs_ptr,
4232 : sequence_control_set_ptr,
4233 : sequence_control_set_ptr->encode_context_ptr,
4234 : context_ptr,
4235 : context_ptr->high_level_rate_control_ptr);
4236 : }
4237 :
4238 : // Frame level RC. Find the ParamPtr for the current GOP
4239 120 : if (sequence_control_set_ptr->intra_period_length == -1 || sequence_control_set_ptr->static_config.rate_control_mode == 0) {
4240 120 : rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
4241 120 : prev_gop_rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
4242 120 : next_gop_rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
4243 : }
4244 : else {
4245 0 : uint32_t interval_index_temp = 0;
4246 0 : EbBool intervalFound = EB_FALSE;
4247 0 : while ((interval_index_temp < PARALLEL_GOP_MAX_NUMBER) && !intervalFound) {
4248 0 : if (picture_control_set_ptr->picture_number >= context_ptr->rate_control_param_queue[interval_index_temp]->first_poc &&
4249 0 : picture_control_set_ptr->picture_number <= context_ptr->rate_control_param_queue[interval_index_temp]->last_poc) {
4250 0 : intervalFound = EB_TRUE;
4251 : }
4252 : else
4253 0 : interval_index_temp++;
4254 : }
4255 0 : CHECK_REPORT_ERROR(
4256 : interval_index_temp != PARALLEL_GOP_MAX_NUMBER,
4257 : sequence_control_set_ptr->encode_context_ptr->app_callback_ptr,
4258 : EB_ENC_RC_ERROR2);
4259 :
4260 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[interval_index_temp];
4261 :
4262 0 : prev_gop_rate_control_param_ptr = (interval_index_temp == 0) ?
4263 0 : context_ptr->rate_control_param_queue[PARALLEL_GOP_MAX_NUMBER - 1] :
4264 0 : context_ptr->rate_control_param_queue[interval_index_temp - 1];
4265 0 : next_gop_rate_control_param_ptr = (interval_index_temp == PARALLEL_GOP_MAX_NUMBER - 1) ?
4266 0 : context_ptr->rate_control_param_queue[0] :
4267 0 : context_ptr->rate_control_param_queue[interval_index_temp + 1];
4268 : }
4269 :
4270 120 : rate_control_layer_ptr = rate_control_param_ptr->rate_control_layer_array[picture_control_set_ptr->temporal_layer_index];
4271 :
4272 120 : if (sequence_control_set_ptr->static_config.rate_control_mode == 0) {
4273 : // if RC mode is 0, fixed QP is used
4274 : // QP scaling based on POC number for Flat IPPP structure
4275 120 : frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[picture_control_set_ptr->picture_qp];
4276 :
4277 120 : if (sequence_control_set_ptr->static_config.enable_qp_scaling_flag && picture_control_set_ptr->parent_pcs_ptr->qp_on_the_fly == EB_FALSE) {
4278 120 : const int32_t qindex = quantizer_to_qindex[(uint8_t)sequence_control_set_ptr->qp];
4279 120 : const double q_val = eb_av1_convert_qindex_to_q(qindex, (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
4280 : // if there are need enough pictures in the LAD/SlidingWindow, the adaptive QP scaling is not used
4281 : #if TWO_PASS
4282 : int32_t new_qindex;
4283 120 : if (!sequence_control_set_ptr->use_output_stat_file && picture_control_set_ptr->parent_pcs_ptr->frames_in_sw >= QPS_SW_THRESH) {
4284 : // Content adaptive qp assignment
4285 106 : if(sequence_control_set_ptr->use_input_stat_file && !picture_control_set_ptr->parent_pcs_ptr->sc_content_detected &&
4286 0 : picture_control_set_ptr->parent_pcs_ptr->referenced_area_has_non_zero)
4287 0 : new_qindex = adaptive_qindex_calc_two_pass(
4288 : picture_control_set_ptr,
4289 : &rc,
4290 : qindex);
4291 : else
4292 106 : new_qindex = adaptive_qindex_calc(
4293 : picture_control_set_ptr,
4294 : &rc,
4295 : qindex);
4296 : }
4297 14 : else if (picture_control_set_ptr->slice_type == I_SLICE) {
4298 0 : const int32_t delta_qindex = eb_av1_compute_qdelta(
4299 : q_val,
4300 : q_val * 0.25,
4301 0 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
4302 :
4303 0 : new_qindex = (int32_t)(qindex + delta_qindex);
4304 : }
4305 : else {
4306 14 : const double delta_rate_new[2][6] =
4307 : { { 0.40, 0.7, 0.85, 1.0, 1.0, 1.0 },
4308 : { 0.35, 0.6, 0.8, 0.9, 1.0, 1.0 } };
4309 :
4310 14 : const int32_t delta_qindex = eb_av1_compute_qdelta(
4311 : q_val,
4312 14 : q_val * delta_rate_new[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels == 4][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index],
4313 14 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
4314 :
4315 14 : new_qindex = (int32_t)(qindex + delta_qindex);
4316 : }
4317 120 : frm_hdr->quantization_params.base_q_idx =
4318 120 : (uint8_t)CLIP3(
4319 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.min_qp_allowed],
4320 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.max_qp_allowed],
4321 : (int32_t)(new_qindex));
4322 : #else
4323 : if (picture_control_set_ptr->parent_pcs_ptr->frames_in_sw >= QPS_SW_THRESH) {
4324 : int32_t new_qindex = adaptive_qindex_calc(
4325 : picture_control_set_ptr,
4326 : &rc,
4327 : qindex);
4328 :
4329 : frm_hdr->quantization_params.base_q_idx =
4330 : (uint8_t)CLIP3(
4331 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.min_qp_allowed],
4332 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.max_qp_allowed],
4333 : (int32_t)(new_qindex));
4334 : }
4335 : else if (picture_control_set_ptr->slice_type == I_SLICE) {
4336 : const int32_t delta_qindex = eb_av1_compute_qdelta(
4337 : q_val,
4338 : q_val * 0.25,
4339 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
4340 : frm_hdr->quantization_params.base_q_idx =
4341 : (uint8_t)CLIP3(
4342 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.min_qp_allowed],
4343 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.max_qp_allowed],
4344 : (int32_t)(qindex + delta_qindex));
4345 : }
4346 : else {
4347 : const double delta_rate_new[2][6] =
4348 : { { 0.40, 0.7, 0.85, 1.0, 1.0, 1.0 },
4349 : { 0.35, 0.6, 0.8, 0.9, 1.0, 1.0 } };
4350 :
4351 : const int32_t delta_qindex = eb_av1_compute_qdelta(
4352 : q_val,
4353 : q_val * delta_rate_new[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels == 4][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index],
4354 : (AomBitDepth)sequence_control_set_ptr->static_config.encoder_bit_depth);
4355 :
4356 : frm_hdr->quantization_params.base_q_idx =
4357 : (uint8_t)CLIP3(
4358 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.min_qp_allowed],
4359 : (int32_t)quantizer_to_qindex[sequence_control_set_ptr->static_config.max_qp_allowed],
4360 : (int32_t)(qindex + delta_qindex));
4361 : }
4362 : #endif
4363 120 : picture_control_set_ptr->picture_qp =
4364 120 : (uint8_t)CLIP3((int32_t)sequence_control_set_ptr->static_config.min_qp_allowed,
4365 : (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed,
4366 : (frm_hdr->quantization_params.base_q_idx + 2) >> 2);
4367 : }
4368 :
4369 0 : else if (picture_control_set_ptr->parent_pcs_ptr->qp_on_the_fly == EB_TRUE) {
4370 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3((int32_t)sequence_control_set_ptr->static_config.min_qp_allowed, (int32_t)sequence_control_set_ptr->static_config.max_qp_allowed, picture_control_set_ptr->parent_pcs_ptr->picture_qp);
4371 0 : frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[picture_control_set_ptr->picture_qp];
4372 : }
4373 :
4374 120 : picture_control_set_ptr->parent_pcs_ptr->picture_qp = picture_control_set_ptr->picture_qp;
4375 120 : setup_segmentation(
4376 : picture_control_set_ptr,
4377 : sequence_control_set_ptr,
4378 : rate_control_layer_ptr
4379 : );
4380 : }
4381 : else {
4382 : // ***Rate Control***
4383 0 : if (sequence_control_set_ptr->static_config.rate_control_mode == 1)
4384 0 : picture_control_set_ptr->picture_qp = rate_control_get_quantizer(rc_model_ptr, picture_control_set_ptr->parent_pcs_ptr);
4385 0 : else if (sequence_control_set_ptr->static_config.rate_control_mode == 2) {
4386 0 : frame_level_rc_input_picture_vbr(
4387 : picture_control_set_ptr,
4388 : sequence_control_set_ptr,
4389 : context_ptr,
4390 : rate_control_layer_ptr,
4391 : rate_control_param_ptr);
4392 :
4393 : // rate control QP refinement
4394 0 : rate_control_refinement(
4395 : picture_control_set_ptr,
4396 : sequence_control_set_ptr,
4397 : rate_control_param_ptr,
4398 : prev_gop_rate_control_param_ptr,
4399 : next_gop_rate_control_param_ptr);
4400 : }
4401 0 : else if (sequence_control_set_ptr->static_config.rate_control_mode == 3) {
4402 0 : frame_level_rc_input_picture_cvbr(
4403 : picture_control_set_ptr,
4404 : sequence_control_set_ptr,
4405 : context_ptr,
4406 : rate_control_layer_ptr,
4407 : rate_control_param_ptr);
4408 : }
4409 0 : picture_control_set_ptr->picture_qp = (uint8_t)CLIP3(
4410 : sequence_control_set_ptr->static_config.min_qp_allowed,
4411 : sequence_control_set_ptr->static_config.max_qp_allowed,
4412 : picture_control_set_ptr->picture_qp);
4413 0 : frm_hdr->quantization_params.base_q_idx = quantizer_to_qindex[picture_control_set_ptr->picture_qp];
4414 : }
4415 :
4416 120 : picture_control_set_ptr->parent_pcs_ptr->picture_qp = picture_control_set_ptr->picture_qp;
4417 :
4418 120 : if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0 && sequence_control_set_ptr->static_config.look_ahead_distance != 0)
4419 10 : context_ptr->base_layer_frames_avg_qp = (3 * context_ptr->base_layer_frames_avg_qp + picture_control_set_ptr->picture_qp + 2) >> 2;
4420 120 : if (picture_control_set_ptr->slice_type == I_SLICE) {
4421 4 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
4422 2 : rate_control_param_ptr->first_pic_pred_qp = (uint16_t)picture_control_set_ptr->parent_pcs_ptr->best_pred_qp;
4423 2 : rate_control_param_ptr->first_pic_actual_qp = (uint16_t)picture_control_set_ptr->picture_qp;
4424 2 : rate_control_param_ptr->scene_change_in_gop = picture_control_set_ptr->parent_pcs_ptr->scene_change_in_gop;
4425 2 : rate_control_param_ptr->first_pic_actual_qp_assigned = EB_TRUE;
4426 : }
4427 : {
4428 4 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
4429 2 : if (sequence_control_set_ptr->static_config.look_ahead_distance != 0)
4430 2 : context_ptr->base_layer_intra_frames_avg_qp = (3 * context_ptr->base_layer_intra_frames_avg_qp + picture_control_set_ptr->picture_qp + 2) >> 2;
4431 : }
4432 :
4433 4 : if (picture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
4434 2 : rate_control_param_ptr->intra_frames_qp = picture_control_set_ptr->picture_qp;
4435 2 : rate_control_param_ptr->next_gop_intra_frame_qp = picture_control_set_ptr->picture_qp;
4436 2 : rate_control_param_ptr->intra_frames_qp_bef_scal = (uint8_t)sequence_control_set_ptr->static_config.max_qp_allowed;
4437 117 : for (uint32_t qindex = sequence_control_set_ptr->static_config.min_qp_allowed; qindex <= sequence_control_set_ptr->static_config.max_qp_allowed; qindex++) {
4438 117 : if (rate_control_param_ptr->intra_frames_qp <= context_ptr->qp_scaling_map_I_SLICE[qindex]) {
4439 2 : rate_control_param_ptr->intra_frames_qp_bef_scal = (uint8_t)qindex;
4440 2 : break;
4441 : }
4442 : }
4443 : }
4444 : }
4445 : }
4446 : #if TWO_PASS
4447 120 : if (sequence_control_set_ptr->static_config.enable_adaptive_quantization == 2 && picture_control_set_ptr->parent_pcs_ptr->frames_in_sw >= QPS_SW_THRESH &&
4448 106 : !picture_control_set_ptr->parent_pcs_ptr->sc_content_detected && !sequence_control_set_ptr->use_output_stat_file)
4449 106 : if(sequence_control_set_ptr->use_input_stat_file && picture_control_set_ptr->parent_pcs_ptr->referenced_area_has_non_zero)
4450 0 : sb_qp_derivation_two_pass(picture_control_set_ptr);
4451 : else
4452 106 : sb_qp_derivation(picture_control_set_ptr);
4453 : else {
4454 14 : picture_control_set_ptr->parent_pcs_ptr->frm_hdr.delta_q_params.delta_q_present = 0;
4455 : LargestCodingUnit *sb_ptr;
4456 14 : picture_control_set_ptr->parent_pcs_ptr->average_qp = 0;
4457 854 : for (int sb_addr = 0; sb_addr < sequence_control_set_ptr->sb_tot_cnt; ++sb_addr) {
4458 840 : sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_addr];
4459 840 : sb_ptr->qp = (uint8_t)picture_control_set_ptr->picture_qp;
4460 840 : sb_ptr->delta_qp = 0;
4461 840 : picture_control_set_ptr->parent_pcs_ptr->average_qp += sb_ptr->qp;
4462 : }
4463 : }
4464 : #else
4465 : sb_qp_derivation(picture_control_set_ptr);
4466 : #endif
4467 : // Get Empty Rate Control Results Buffer
4468 120 : eb_get_empty_object(
4469 : context_ptr->rate_control_output_results_fifo_ptr,
4470 : &rate_control_results_wrapper_ptr);
4471 120 : rate_control_results_ptr = (RateControlResults*)rate_control_results_wrapper_ptr->object_ptr;
4472 120 : rate_control_results_ptr->picture_control_set_wrapper_ptr = rate_control_tasks_ptr->picture_control_set_wrapper_ptr;
4473 :
4474 : // Post Full Rate Control Results
4475 120 : eb_post_full_object(rate_control_results_wrapper_ptr);
4476 :
4477 : // Release Rate Control Tasks
4478 120 : eb_release_object(rate_control_tasks_wrapper_ptr);
4479 :
4480 120 : break;
4481 :
4482 120 : case RC_PACKETIZATION_FEEDBACK_RESULT:
4483 :
4484 120 : parentpicture_control_set_ptr = (PictureParentControlSet *)rate_control_tasks_ptr->picture_control_set_wrapper_ptr->object_ptr;
4485 120 : sequence_control_set_ptr = (SequenceControlSet *)parentpicture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
4486 120 : if (sequence_control_set_ptr->static_config.rate_control_mode) {
4487 0 : if (sequence_control_set_ptr->static_config.rate_control_mode == 1)
4488 0 : rate_control_update_model(rc_model_ptr, parentpicture_control_set_ptr);
4489 : ReferenceQueueEntry *reference_entry_ptr;
4490 : uint32_t reference_queue_index;
4491 0 : EncodeContext *encode_context_ptr = sequence_control_set_ptr->encode_context_ptr;
4492 0 : reference_queue_index = encode_context_ptr->reference_picture_queue_head_index;
4493 : // Find the Reference in the Reference Queue
4494 : do {
4495 0 : reference_entry_ptr = encode_context_ptr->reference_picture_queue[reference_queue_index];
4496 0 : if (reference_entry_ptr->picture_number == parentpicture_control_set_ptr->picture_number) {
4497 : // Set the feedback arrived
4498 0 : reference_entry_ptr->feedback_arrived = EB_TRUE;
4499 : }
4500 :
4501 : // Increment the reference_queue_index Iterator
4502 0 : reference_queue_index = (reference_queue_index == REFERENCE_QUEUE_MAX_DEPTH - 1) ? 0 : reference_queue_index + 1;
4503 0 : } while ((reference_queue_index != encode_context_ptr->reference_picture_queue_tail_index) && (reference_entry_ptr->picture_number != parentpicture_control_set_ptr->picture_number));
4504 : }
4505 : // Frame level RC
4506 120 : if (sequence_control_set_ptr->intra_period_length == -1 || sequence_control_set_ptr->static_config.rate_control_mode == 0) {
4507 120 : rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
4508 120 : prev_gop_rate_control_param_ptr = context_ptr->rate_control_param_queue[0];
4509 120 : if (parentpicture_control_set_ptr->slice_type == I_SLICE) {
4510 4 : if (parentpicture_control_set_ptr->total_num_bits > MAX_BITS_PER_FRAME)
4511 0 : context_ptr->max_rate_adjust_delta_qp++;
4512 4 : else if (context_ptr->max_rate_adjust_delta_qp > 0 && parentpicture_control_set_ptr->total_num_bits < MAX_BITS_PER_FRAME * 85 / 100)
4513 0 : context_ptr->max_rate_adjust_delta_qp--;
4514 4 : context_ptr->max_rate_adjust_delta_qp = CLIP3(0, 63, context_ptr->max_rate_adjust_delta_qp);
4515 4 : context_ptr->max_rate_adjust_delta_qp = 0;
4516 : }
4517 : }
4518 : else {
4519 0 : uint32_t interval_index_temp = 0;
4520 0 : EbBool intervalFound = EB_FALSE;
4521 0 : while ((interval_index_temp < PARALLEL_GOP_MAX_NUMBER) && !intervalFound) {
4522 0 : if (parentpicture_control_set_ptr->picture_number >= context_ptr->rate_control_param_queue[interval_index_temp]->first_poc &&
4523 0 : parentpicture_control_set_ptr->picture_number <= context_ptr->rate_control_param_queue[interval_index_temp]->last_poc) {
4524 0 : intervalFound = EB_TRUE;
4525 : }
4526 : else
4527 0 : interval_index_temp++;
4528 : }
4529 0 : CHECK_REPORT_ERROR(
4530 : interval_index_temp != PARALLEL_GOP_MAX_NUMBER,
4531 : sequence_control_set_ptr->encode_context_ptr->app_callback_ptr,
4532 : EB_ENC_RC_ERROR2);
4533 :
4534 0 : rate_control_param_ptr = context_ptr->rate_control_param_queue[interval_index_temp];
4535 :
4536 0 : prev_gop_rate_control_param_ptr = (interval_index_temp == 0) ?
4537 0 : context_ptr->rate_control_param_queue[PARALLEL_GOP_MAX_NUMBER - 1] :
4538 0 : context_ptr->rate_control_param_queue[interval_index_temp - 1];
4539 : }
4540 120 : if (sequence_control_set_ptr->static_config.rate_control_mode != 0) {
4541 0 : context_ptr->previous_virtual_buffer_level = context_ptr->virtual_buffer_level;
4542 :
4543 0 : context_ptr->virtual_buffer_level =
4544 0 : (int64_t)context_ptr->previous_virtual_buffer_level +
4545 0 : (int64_t)parentpicture_control_set_ptr->total_num_bits - (int64_t)context_ptr->high_level_rate_control_ptr->channel_bit_rate_per_frame;
4546 :
4547 0 : high_level_rc_feed_back_picture(
4548 : parentpicture_control_set_ptr,
4549 : sequence_control_set_ptr);
4550 0 : if (sequence_control_set_ptr->static_config.rate_control_mode == 2)
4551 0 : frame_level_rc_feedback_picture_vbr(
4552 : parentpicture_control_set_ptr,
4553 : sequence_control_set_ptr,
4554 : context_ptr);
4555 0 : else if (sequence_control_set_ptr->static_config.rate_control_mode == 3)
4556 0 : frame_level_rc_feedback_picture_cvbr(
4557 : parentpicture_control_set_ptr,
4558 : sequence_control_set_ptr,
4559 : context_ptr);
4560 0 : if (parentpicture_control_set_ptr->picture_number == rate_control_param_ptr->first_poc) {
4561 0 : rate_control_param_ptr->first_pic_pred_bits = parentpicture_control_set_ptr->target_bits_best_pred_qp;
4562 0 : rate_control_param_ptr->first_pic_actual_bits = parentpicture_control_set_ptr->total_num_bits;
4563 : {
4564 0 : int16_t deltaApQp = (int16_t)rate_control_param_ptr->first_pic_actual_qp - (int16_t)rate_control_param_ptr->first_pic_pred_qp;
4565 0 : rate_control_param_ptr->extra_ap_bit_ratio_i = (rate_control_param_ptr->first_pic_pred_bits != 0) ?
4566 0 : (((int64_t)rate_control_param_ptr->first_pic_actual_bits - (int64_t)rate_control_param_ptr->first_pic_pred_bits) * 100) / ((int64_t)rate_control_param_ptr->first_pic_pred_bits) :
4567 : 0;
4568 0 : rate_control_param_ptr->extra_ap_bit_ratio_i += (int64_t)deltaApQp * 15;
4569 : }
4570 : }
4571 : }
4572 :
4573 : // Queue variables
4574 : #if OVERSHOOT_STAT_PRINT
4575 : if (sequence_control_set_ptr->intra_period_length != -1) {
4576 : int32_t queueEntryIndex;
4577 : uint32_t queueEntryIndexTemp;
4578 : uint32_t queueEntryIndexTemp2;
4579 : CodedFramesStatsEntry *queueEntryPtr;
4580 : EbBool moveSlideWondowFlag = EB_TRUE;
4581 : EbBool end_of_sequence_flag = EB_TRUE;
4582 : uint32_t frames_in_sw;
4583 :
4584 : // Determine offset from the Head Ptr
4585 : queue_entry_index = (int32_t)(parentpicture_control_set_ptr->picture_number - context_ptr->coded_frames_stat_queue[context_ptr->coded_frames_stat_queue_head_index]->picture_number);
4586 : queue_entry_index += context_ptr->coded_frames_stat_queue_head_index;
4587 : queue_entry_index = (queue_entry_index > CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? queue_entry_index - CODED_FRAMES_STAT_QUEUE_MAX_DEPTH : queue_entry_index;
4588 : queue_entry_ptr = context_ptr->coded_frames_stat_queue[queue_entry_index];
4589 :
4590 : queue_entry_ptr->frame_total_bit_actual = (uint64_t)parentpicture_control_set_ptr->total_num_bits;
4591 : queue_entry_ptr->picture_number = parentpicture_control_set_ptr->picture_number;
4592 : queue_entry_ptr->end_of_sequence_flag = parentpicture_control_set_ptr->end_of_sequence_flag;
4593 : context_ptr->rate_average_periodin_frames = (uint64_t)sequence_control_set_ptr->static_config.intra_period + 1;
4594 :
4595 : //SVT_LOG("\n0_POC: %d\n",
4596 : // queue_entry_ptr->picture_number);
4597 : move_slide_wondow_flag = EB_TRUE;
4598 : while (move_slide_wondow_flag) {
4599 : // SVT_LOG("\n1_POC: %d\n",
4600 : // queue_entry_ptr->picture_number);
4601 : // Check if the sliding window condition is valid
4602 : queue_entry_index_temp = context_ptr->coded_frames_stat_queue_head_index;
4603 : if (context_ptr->coded_frames_stat_queue[queue_entry_index_temp]->frame_total_bit_actual != -1)
4604 : end_of_sequence_flag = context_ptr->coded_frames_stat_queue[queue_entry_index_temp]->end_of_sequence_flag;
4605 : else
4606 : end_of_sequence_flag = EB_FALSE;
4607 : while (move_slide_wondow_flag && !end_of_sequence_flag &&
4608 : queue_entry_index_temp < context_ptr->coded_frames_stat_queue_head_index + context_ptr->rate_average_periodin_frames) {
4609 : // SVT_LOG("\n2_POC: %d\n",
4610 : // queue_entry_ptr->picture_number);
4611 :
4612 : queue_entry_index_temp2 = (queue_entry_index_temp > CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - CODED_FRAMES_STAT_QUEUE_MAX_DEPTH : queue_entry_index_temp;
4613 :
4614 : move_slide_wondow_flag = (EbBool)(move_slide_wondow_flag && (context_ptr->coded_frames_stat_queue[queue_entry_index_temp2]->frame_total_bit_actual != -1));
4615 :
4616 : if (context_ptr->coded_frames_stat_queue[queue_entry_index_temp2]->frame_total_bit_actual != -1) {
4617 : // check if it is the last frame. If we have reached the last frame, we would output the buffered frames in the Queue.
4618 : end_of_sequence_flag = context_ptr->coded_frames_stat_queue[queue_entry_index_temp]->end_of_sequence_flag;
4619 : }
4620 : else
4621 : end_of_sequence_flag = EB_FALSE;
4622 : queue_entry_index_temp =
4623 : (queue_entry_index_temp == CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? 0 : queue_entry_index_temp + 1;
4624 : }
4625 :
4626 : if (move_slide_wondow_flag) {
4627 : //get a new entry spot
4628 : queue_entry_ptr = (context_ptr->coded_frames_stat_queue[context_ptr->coded_frames_stat_queue_head_index]);
4629 : queue_entry_index_temp = context_ptr->coded_frames_stat_queue_head_index;
4630 : // This is set to false, so the last frame would go inside the loop
4631 : end_of_sequence_flag = EB_FALSE;
4632 : frames_in_sw = 0;
4633 : context_ptr->total_bit_actual_per_sw = 0;
4634 :
4635 : while (!end_of_sequence_flag &&
4636 : queue_entry_index_temp < context_ptr->coded_frames_stat_queue_head_index + context_ptr->rate_average_periodin_frames) {
4637 : frames_in_sw++;
4638 :
4639 : queue_entry_index_temp2 = (queue_entry_index_temp > CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? queue_entry_index_temp - CODED_FRAMES_STAT_QUEUE_MAX_DEPTH : queue_entry_index_temp;
4640 :
4641 : context_ptr->total_bit_actual_per_sw += context_ptr->coded_frames_stat_queue[queue_entry_index_temp2]->frame_total_bit_actual;
4642 : end_of_sequence_flag = context_ptr->coded_frames_stat_queue[queue_entry_index_temp2]->end_of_sequence_flag;
4643 :
4644 : queue_entry_index_temp =
4645 : (queue_entry_index_temp == CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? 0 : queue_entry_index_temp + 1;
4646 : }
4647 : //
4648 :
4649 : //if(frames_in_sw == context_ptr->rate_average_periodin_frames)
4650 : // SVT_LOG("POC:%d\t %.3f\n", queue_entry_ptr->picture_number, (double)context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate>> RC_PRECISION)/(double)frames_in_sw/1000);
4651 : if (frames_in_sw == (uint32_t)sequence_control_set_ptr->intra_period_length + 1) {
4652 : context_ptr->max_bit_actual_per_sw = MAX(context_ptr->max_bit_actual_per_sw, context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate >> RC_PRECISION) / frames_in_sw / 1000);
4653 : if (queue_entry_ptr->picture_number % ((sequence_control_set_ptr->intra_period_length + 1)) == 0) {
4654 : context_ptr->max_bit_actual_per_gop = MAX(context_ptr->max_bit_actual_per_gop, context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate >> RC_PRECISION) / frames_in_sw / 1000);
4655 : context_ptr->min_bit_actual_per_gop = MIN(context_ptr->min_bit_actual_per_gop, context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate >> RC_PRECISION) / frames_in_sw / 1000);
4656 : if (1) {
4657 : //if (context_ptr->total_bit_actual_per_sw > sequence_control_set_ptr->static_config.max_buffersize){
4658 : SVT_LOG("POC:%d\t%.0f\t%.2f%% \n",
4659 : (int)queue_entry_ptr->picture_number,
4660 : (double)((int64_t)context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate >> RC_PRECISION) / frames_in_sw / 1000),
4661 : (double)(100 * (double)context_ptr->total_bit_actual_per_sw*(sequence_control_set_ptr->frame_rate >> RC_PRECISION) / frames_in_sw / (double)sequence_control_set_ptr->static_config.target_bit_rate) - 100);
4662 : }
4663 : }
4664 : }
4665 : if (frames_in_sw == context_ptr->rate_average_periodin_frames - 1) {
4666 : //SVT_LOG("\n%d MAX\n", (int32_t)context_ptr->max_bit_actual_per_sw);
4667 : SVT_LOG("\n%d GopMax\t", (int32_t)context_ptr->max_bit_actual_per_gop);
4668 : SVT_LOG("%d GopMin\n", (int32_t)context_ptr->min_bit_actual_per_gop);
4669 : }
4670 : // Reset the Queue Entry
4671 : queue_entry_ptr->picture_number += CODED_FRAMES_STAT_QUEUE_MAX_DEPTH;
4672 : queue_entry_ptr->frame_total_bit_actual = -1;
4673 :
4674 : // Increment the Reorder Queue head Ptr
4675 : context_ptr->coded_frames_stat_queue_head_index =
4676 : (context_ptr->coded_frames_stat_queue_head_index == CODED_FRAMES_STAT_QUEUE_MAX_DEPTH - 1) ? 0 : context_ptr->coded_frames_stat_queue_head_index + 1;
4677 :
4678 : queue_entry_ptr = (context_ptr->coded_frames_stat_queue[context_ptr->coded_frames_stat_queue_head_index]);
4679 : }
4680 : }
4681 : }
4682 : #endif
4683 120 : total_number_of_fb_frames++;
4684 :
4685 : // Release the SequenceControlSet
4686 120 : eb_release_object(parentpicture_control_set_ptr->sequence_control_set_wrapper_ptr);
4687 : // Release the ParentPictureControlSet
4688 120 : eb_release_object(parentpicture_control_set_ptr->input_picture_wrapper_ptr);
4689 120 : eb_release_object(rate_control_tasks_ptr->picture_control_set_wrapper_ptr);
4690 :
4691 : // Release Rate Control Tasks
4692 120 : eb_release_object(rate_control_tasks_wrapper_ptr);
4693 120 : break;
4694 :
4695 720 : case RC_ENTROPY_CODING_ROW_FEEDBACK_RESULT:
4696 :
4697 : // Extract bits-per-lcu-row
4698 :
4699 : // Release Rate Control Tasks
4700 720 : eb_release_object(rate_control_tasks_wrapper_ptr);
4701 :
4702 720 : break;
4703 :
4704 0 : default:
4705 0 : picture_control_set_ptr = (PictureControlSet*)rate_control_tasks_ptr->picture_control_set_wrapper_ptr->object_ptr;
4706 0 : sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
4707 :
4708 0 : break;
4709 : }
4710 : }
4711 : EB_DELETE(rc_model_ptr);
4712 : return EB_NULL;
4713 : }
|