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