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 "EbUtility.h"
10 #include <math.h>
11 
12 #include "EbSequenceControlSet.h"
13 #include "EbPictureControlSet.h"
14 
15 #include "EbModeDecisionConfigurationProcess.h"
16 #include "EbRateControlResults.h"
17 #include "EbEncDecTasks.h"
18 #include "EbReferenceObject.h"
19 #include "EbEncDecProcess.h"
20 #include "EbRateDistortionCost.h"
21 #include "stdint.h"
22 
23 #if SEG_SUPPORT
24 #include "vp9_segmentation.h"
25 #endif
26 
27 // Shooting states
28 #define UNDER_SHOOTING                        0
29 #define OVER_SHOOTING                         1
30 #define TBD_SHOOTING                          2
31 
32 // Set a cost to each search method (could be modified)
33 // EB30 @ Revision 12879
34 #define PRED_OPEN_LOOP_1_NFL_COST    97 // PRED_OPEN_LOOP_1_NFL_COST is ~03% faster than PRED_OPEN_LOOP_COST
35 #define U_099                        99
36 #define PRED_OPEN_LOOP_COST         100 // Let's assume PRED_OPEN_LOOP_COST costs ~100 U
37 #define U_101                       101
38 #define U_102                       102
39 #define U_103                       103
40 #define U_104                       104
41 #define U_105                       105
42 #define LIGHT_OPEN_LOOP_COST        106 // L_MDC is ~06% slower than PRED_OPEN_LOOP_COST
43 #define U_107                       107
44 #define U_108                       108
45 #define U_109                       109
46 #define OPEN_LOOP_COST              110 // F_MDC is ~10% slower than PRED_OPEN_LOOP_COST
47 #define U_111                       111
48 #define U_112                       112
49 #define U_113                       113
50 #define U_114                       114
51 #define U_115                       115
52 #define U_116                       116
53 #define U_117                       117
54 #define U_119                       119
55 #define U_120                       120
56 #define U_121                       121
57 #define U_122                       122
58 #define LIGHT_AVC_COST              122
59 #define LIGHT_BDP_COST              123 // L_BDP is ~23% slower than PRED_OPEN_LOOP_COST
60 #define U_125                       125
61 #define U_127                       127
62 #define BDP_COST                    129 // F_BDP is ~29% slower than PRED_OPEN_LOOP_COST
63 #define U_130                       130
64 #define U_132                       132
65 #define U_133                       133
66 #define U_134                       134
67 #define AVC_COST                    138 // L_BDP is ~38% slower than PRED_OPEN_LOOP_COST
68 #define U_140                       140
69 #define U_145                       145
70 #define U_150                       150
71 #define U_152                       152
72 #define FULL_SEARCH_COST            155 // FS    is ~55% slower than PRED_OPEN_LOOP_COST
73 
74 // ADP LCU score Manipulation
75 #define ADP_CLASS_SHIFT_DIST_0    50
76 #define ADP_CLASS_SHIFT_DIST_1    75
77 
78 #define ADP_BLACK_AREA_PERCENTAGE 25
79 #define ADP_DARK_SB_TH           25
80 
81 #define ADP_CLASS_NON_MOVING_INDEX_TH_0 10
82 #define ADP_CLASS_NON_MOVING_INDEX_TH_1 25
83 #define ADP_CLASS_NON_MOVING_INDEX_TH_2 30
84 
85 #define ADP_CONFIG_NON_MOVING_INDEX_TH_0 15
86 #define ADP_CONFIG_NON_MOVING_INDEX_TH_1 30
87 
88 static const uint8_t adp_luminosity_change_th_array[MAX_HIERARCHICAL_LEVEL][MAX_TEMPORAL_LAYERS] = { // [Highest Temporal Layer] [Temporal Layer Index]
89     {  2 },
90     {  2, 2 },
91     {  3, 2, 2 },
92     {  3, 3, 2, 2 },
93     {  4, 3, 3, 2, 2 },
94     {  4, 4, 3, 3, 2, 2 },
95 };
96 
97 #define HIGH_HOMOGENOUS_PIC_TH               30
98 
99 #define US_ADJ_STEP_TH0                      7
100 #define US_ADJ_STEP_TH1                      5
101 #define US_ADJ_STEP_TH2                      2
102 #define OS_ADJ_STEP_TH1                      2
103 #define OS_ADJ_STEP_TH2                      5
104 #define OS_ADJ_STEP_TH3                      7
105 
106 #define VALID_SLOT_TH                        2
107 
108 /******************************************************
109  * Mode Decision Configuration Context Constructor
110  ******************************************************/
eb_vp9_mode_decision_configuration_context_ctor(ModeDecisionConfigurationContext ** context_dbl_ptr,EbFifo * rate_control_input_fifo_ptr,EbFifo * mode_decision_configuration_output_fifo_ptr,uint16_t sb_total_count)111 EbErrorType eb_vp9_mode_decision_configuration_context_ctor(
112     ModeDecisionConfigurationContext **context_dbl_ptr,
113     EbFifo                            *rate_control_input_fifo_ptr,
114     EbFifo                            *mode_decision_configuration_output_fifo_ptr,
115     uint16_t                           sb_total_count)
116 
117 {
118     ModeDecisionConfigurationContext *context_ptr;
119 
120     EB_MALLOC(ModeDecisionConfigurationContext*, context_ptr, sizeof(ModeDecisionConfigurationContext), EB_N_PTR);
121 
122     *context_dbl_ptr = context_ptr;
123 
124     // Input/Output System Resource Manager FIFOs
125     context_ptr->rate_control_input_fifo_ptr                = rate_control_input_fifo_ptr;
126     context_ptr->mode_decision_configuration_output_fifo_ptr = mode_decision_configuration_output_fifo_ptr;
127 
128     // Budgeting
129     EB_MALLOC(uint32_t*,context_ptr->sb_score_array, sizeof(uint32_t) * sb_total_count, EB_N_PTR);
130     EB_MALLOC(uint8_t *,context_ptr->sb_cost_array , sizeof(uint8_t ) * sb_total_count, EB_N_PTR);
131 
132     // Open Loop Partitioning
133     EB_MALLOC(ModeDecisionCandidate*, context_ptr->candidate_ptr, sizeof(ModeDecisionCandidate), EB_N_PTR);
134     EB_MALLOC(ModeInfo*, context_ptr->candidate_ptr->mode_info, sizeof(ModeInfo), EB_N_PTR);
135 
136     EB_MALLOC(MACROBLOCKD*, context_ptr->e_mbd, sizeof(MACROBLOCKD), EB_N_PTR);
137 
138     EB_MALLOC(MbModeInfoExt*, context_ptr->mbmi_ext, sizeof(MbModeInfoExt), EB_N_PTR);
139     EB_MEMSET(context_ptr->mbmi_ext, 0, sizeof(MbModeInfoExt)); // Hsan: reference MVs not generated @ MDC (i.e. always (0,0))
140 
141     return EB_ErrorNone;
142 }
143 
144 /******************************************************
145 * Detect complex/non-flat/moving LCU in a non-complex area (used to refine MDC depth control)
146 ******************************************************/
complex_non_flat_moving_sb(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t picture_width_in_sb)147 void complex_non_flat_moving_sb(
148     SequenceControlSet *sequence_control_set_ptr,
149     PictureControlSet  *picture_control_set_ptr,
150     uint32_t            picture_width_in_sb) {
151 
152     SbUnit *sb_ptr;
153     uint32_t sb_index;
154 
155     if (picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score != INVALID_NON_MOVING_SCORE && picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score >= 10 && picture_control_set_ptr->temporal_layer_index <= 2) {
156 
157         // Determine deltaQP and assign QP value for each leaf
158         for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
159 
160             SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
161 
162             sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
163 
164             EB_BOOL condition = EB_FALSE;
165 
166             if (!picture_control_set_ptr->parent_pcs_ptr->similar_colocated_sb_array[sb_index] &&
167                 sb_ptr->picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index].edge_block_num > 0) {
168                 condition = EB_TRUE;
169             }
170 
171             if (condition){
172                 uint32_t  counter = 0;
173 
174                 if (!sb_params->is_edge_sb){
175                     // Top
176                     if (picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index - picture_width_in_sb].edge_block_num == 0)
177                         counter++;
178                     // Bottom
179                     if (picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index + picture_width_in_sb].edge_block_num == 0)
180                         counter++;
181                     // Left
182                     if (picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index - 1].edge_block_num == 0)
183                         counter++;
184                     // right
185                     if (picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index + 1].edge_block_num == 0)
186                         counter++;
187                 }
188             }
189         }
190     }
191 }
192 
aura_detection64x64(PictureControlSet * picture_control_set_ptr,uint8_t picture_qp,uint32_t sb_index)193 EB_AURA_STATUS aura_detection64x64(
194     PictureControlSet *picture_control_set_ptr,
195     uint8_t            picture_qp,
196     uint32_t           sb_index
197     )
198 {
199 
200     SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
201     int32_t             picture_width_in_sb = (sequence_control_set_ptr->luma_width + MAX_SB_SIZE_MINUS_1) >> MAX_LOG2_SB_SIZE;
202     uint32_t            curr_dist;
203     uint32_t            top_dist, top_l_dist, top_r_dist;
204     uint32_t            local_avg_dist, dist_thresh0, dist_thresh1;
205     int32_t             sb_offset;
206 
207     EB_AURA_STATUS      aura_class = AURA_STATUS_0;
208     uint8_t             aura_class1 = 0;
209     uint8_t             aura_class2 = 0;
210 
211     int32_t             x_mv0 = 0;
212     int32_t             y_mv0 = 0;
213     int32_t             x_mv1 = 0;
214     int32_t             y_mv1 = 0;
215 
216     uint32_t            left_dist, right_dist;
217 
218     SbParams           *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
219 
220     dist_thresh0 = picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag ||sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE ? 15 : 14;
221     dist_thresh1 = 23;
222 
223     if (picture_qp > 38){
224         dist_thresh0 = dist_thresh0 << 2;
225         dist_thresh1 = dist_thresh1 << 2;
226     }
227 
228     if (!sb_params->is_edge_sb){
229 
230         uint32_t k;
231 
232         MeCuResults *me_pu_result = &picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index][0];
233 
234         //Curr Block
235 
236         for (k = 0; k < me_pu_result->total_me_candidate_index; k++) {
237 
238             if (me_pu_result->distortion_direction[k].direction == UNI_PRED_LIST_0) {
239                 // Get reference list 0 / reference index 0 MV
240                 x_mv0 = me_pu_result->x_mv_l0;
241                 y_mv0 = me_pu_result->y_mv_l0;
242             }
243             if (me_pu_result->distortion_direction[k].direction == UNI_PRED_LIST_1) {
244                 // Get reference list  1 / reference index 0 MV
245                 x_mv1 = me_pu_result->x_mv_l1;
246                 y_mv1 = me_pu_result->y_mv_l1;
247             }
248 
249         }
250         curr_dist = me_pu_result->distortion_direction[0].distortion;
251 
252         if ((curr_dist > 64 * 64) &&
253             // Only mark a block as aura when it is moving (MV amplitude higher than X; X is temporal layer dependent)
254             (abs(x_mv0) > global_motion_threshold[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index] ||
255             abs(y_mv0) > global_motion_threshold[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index] ||
256             abs(x_mv1) > global_motion_threshold[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index] ||
257             abs(y_mv1) > global_motion_threshold[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]))
258         {
259 
260             //Top Distortion
261             sb_offset = -picture_width_in_sb;
262             top_dist = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index + sb_offset]->distortion_direction[0].distortion;
263 
264             //TopLeft Distortion
265             sb_offset = -picture_width_in_sb - 1;
266             top_l_dist = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index + sb_offset]->distortion_direction[0].distortion;
267 
268             //TopRightDistortion
269             sb_offset = -picture_width_in_sb + 1;
270             top_r_dist = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index + sb_offset]->distortion_direction[0].distortion;
271 
272             top_r_dist = (sb_params->horizontal_index < (uint32_t)(picture_width_in_sb - 2)) ? top_r_dist : curr_dist;
273 
274             //left Distortion
275             sb_offset = -1;
276             left_dist = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index + sb_offset]->distortion_direction[0].distortion;
277 
278             //RightDistortion
279             sb_offset = 1;
280             right_dist = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index + sb_offset]->distortion_direction[0].distortion;
281 
282             right_dist = (sb_params->horizontal_index < (uint32_t)(picture_width_in_sb - 2)) ? top_r_dist : curr_dist;
283 
284             local_avg_dist = MIN(MIN(MIN(top_l_dist, MIN(top_r_dist, top_dist)), left_dist), right_dist);
285 
286             if (10 * curr_dist > dist_thresh0*local_avg_dist){
287                 if (10 * curr_dist > dist_thresh1*local_avg_dist)
288                     aura_class2++;
289                 else
290                     aura_class1++;
291             }
292         }
293 
294     }
295 
296     aura_class = (aura_class2 > 0 || aura_class1 > 0) ? AURA_STATUS_1 : AURA_STATUS_0;
297 
298     return   aura_class;
299 
300 }
301 
302 /******************************************************
303 * Aura detection
304 ******************************************************/
aura_detection(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr)305 void aura_detection(
306     SequenceControlSet *sequence_control_set_ptr,
307     PictureControlSet  *picture_control_set_ptr)
308 {
309     uint32_t sb_index;
310 
311     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
312 
313         SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
314         SbUnit   *sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
315 
316         // Aura status intialization
317         sb_ptr->aura_status = INVALID_AURA_STATUS;
318 
319         if (picture_control_set_ptr->slice_type == B_SLICE){
320             if (!sb_params->is_edge_sb){
321                 sb_ptr->aura_status = aura_detection64x64(
322                     picture_control_set_ptr,
323                     (uint8_t)picture_control_set_ptr->picture_qp,
324                     sb_ptr->sb_index);
325             }
326         }
327     }
328 return;
329 }
330 
eb_vp9_derive_default_segments(PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)331 EbErrorType eb_vp9_derive_default_segments(
332     PictureControlSet                *picture_control_set_ptr,
333     ModeDecisionConfigurationContext *context_ptr)
334 {
335     EbErrorType return_error = EB_ErrorNone;
336 
337     // @ BASE MDC is not considered as similar to BDP_L in term of speed
338     if (picture_control_set_ptr->temporal_layer_index == 0) {
339 
340         if (context_ptr->adp_depth_sensitive_picture_class && context_ptr->budget >= (uint32_t)(picture_control_set_ptr->parent_pcs_ptr->sb_total_count * LIGHT_BDP_COST)) {
341 
342             if (context_ptr->budget > (uint32_t) (picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST)) {
343                 context_ptr->number_of_segments = 2;
344                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
345                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
346                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_FULL84_DEPTH_MODE - 1];
347             }
348             else {
349                 context_ptr->number_of_segments = 2;
350                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
351                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
352                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
353             }
354 
355         }
356         else {
357             if (context_ptr->budget > (uint32_t) (picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST)) {
358 
359                 context_ptr->number_of_segments = 2;
360 
361                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
362 
363                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
364                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_FULL84_DEPTH_MODE - 1];
365             }
366 
367             else if (context_ptr->budget > (uint32_t)(picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST)) {
368 
369                 context_ptr->number_of_segments = 4;
370 
371                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
372                 context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
373                 context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
374 
375                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
376                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
377                 context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
378                 context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
379             }
380             else {
381                 context_ptr->number_of_segments = 5;
382 
383                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
384                 context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
385                 context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
386                 context_ptr->score_th[3] = (int8_t)((4 * 100) / context_ptr->number_of_segments);
387 
388                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE - 1];
389                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
390                 context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
391                 context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
392                 context_ptr->interval_cost[4] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
393             }
394 
395         }
396     }
397     else {
398 
399         if (context_ptr->budget > (uint32_t)(picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_120)) {
400 
401             context_ptr->number_of_segments = 6;
402 
403             context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
404             context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
405             context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
406             context_ptr->score_th[3] = (int8_t)((4 * 100) / context_ptr->number_of_segments);
407             context_ptr->score_th[4] = (int8_t)((5 * 100) / context_ptr->number_of_segments);
408 
409             context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
410             context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
411             context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1];
412             context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
413             context_ptr->interval_cost[4] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
414             context_ptr->interval_cost[5] = context_ptr->cost_depth_mode[SB_FULL85_DEPTH_MODE - 1];
415         }
416         else if (context_ptr->budget > (uint32_t)(picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_115)) {
417 
418                 context_ptr->number_of_segments = 5;
419 
420                 context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
421                 context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
422                 context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
423                 context_ptr->score_th[3] = (int8_t)((4 * 100) / context_ptr->number_of_segments);
424 
425                 context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
426                 context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
427                 context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1];
428                 context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
429                 context_ptr->interval_cost[4] = context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1];
430 
431         } else if (context_ptr->budget > (uint32_t)(picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST)) {
432 
433             context_ptr->number_of_segments = 4;
434 
435             context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
436             context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
437             context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
438 
439             context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
440             context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
441             context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1];
442             context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1];
443 
444         } else {
445 
446             context_ptr->number_of_segments = 4;
447 
448             context_ptr->score_th[0] = (int8_t)((1 * 100) / context_ptr->number_of_segments);
449             context_ptr->score_th[1] = (int8_t)((2 * 100) / context_ptr->number_of_segments);
450             context_ptr->score_th[2] = (int8_t)((3 * 100) / context_ptr->number_of_segments);
451 
452             context_ptr->interval_cost[0] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE - 1];
453             context_ptr->interval_cost[1] = context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1];
454             context_ptr->interval_cost[2] = context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1];
455             context_ptr->interval_cost[3] = context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1];
456         }
457     }
458 
459     return return_error;
460 }
461 
462 /******************************************************
463 * Set the target budget
464     Input   : cost per depth
465     Output  : budget per picture
466 ******************************************************/
467 
SetTargetBudgetSq(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)468 void SetTargetBudgetSq(
469     SequenceControlSet               *sequence_control_set_ptr,
470     PictureControlSet                *picture_control_set_ptr,
471     ModeDecisionConfigurationContext *context_ptr)
472 {
473     uint32_t budget;
474 
475     if (context_ptr->adp_level <= ENC_MODE_4) {
476         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
477             if (picture_control_set_ptr->temporal_layer_index == 0)
478                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
479             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
480                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
481                 picture_control_set_ptr->sb_total_count * U_152 :
482                 picture_control_set_ptr->sb_total_count * U_150;
483             else
484                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
485                 picture_control_set_ptr->sb_total_count * U_152 :
486                 picture_control_set_ptr->sb_total_count * U_145;
487         }
488         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
489             if (picture_control_set_ptr->temporal_layer_index == 0)
490                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
491             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
492                 budget = picture_control_set_ptr->sb_total_count * AVC_COST;
493             else
494                 budget = picture_control_set_ptr->sb_total_count * U_134;
495         }
496         else {
497             if (picture_control_set_ptr->temporal_layer_index == 0)
498                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
499             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
500                 budget = picture_control_set_ptr->sb_total_count * U_132;
501             else
502                 budget = picture_control_set_ptr->sb_total_count * U_121;
503 
504         }
505     }
506     else if (context_ptr->adp_level == ENC_MODE_5) {
507         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
508             if (picture_control_set_ptr->temporal_layer_index == 0)
509                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
510             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
511                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
512                 picture_control_set_ptr->sb_total_count * U_152 :
513                 picture_control_set_ptr->sb_total_count * U_150;
514             else
515                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
516                 picture_control_set_ptr->sb_total_count * U_152 :
517                 picture_control_set_ptr->sb_total_count * U_145;
518         }
519         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
520             if (picture_control_set_ptr->temporal_layer_index == 0)
521                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
522             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
523                 budget = picture_control_set_ptr->sb_total_count * AVC_COST;
524             else
525                 budget = picture_control_set_ptr->sb_total_count * U_134;
526 
527         }
528         else {
529 
530             if (picture_control_set_ptr->temporal_layer_index == 0)
531                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
532             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
533                 budget = picture_control_set_ptr->sb_total_count * U_125;
534             else
535                 budget = picture_control_set_ptr->sb_total_count * U_121;
536 
537         }
538 
539     }
540 
541     else if (context_ptr->adp_level == ENC_MODE_6) {
542         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_576p_RANGE_OR_LOWER) {
543             if (picture_control_set_ptr->temporal_layer_index == 0)
544                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
545             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
546                 budget = picture_control_set_ptr->sb_total_count * U_133;
547             else
548                 budget = picture_control_set_ptr->sb_total_count * U_120;
549         }
550         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
551             if (picture_control_set_ptr->temporal_layer_index == 0)
552                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
553             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
554                 budget = picture_control_set_ptr->sb_total_count * U_113;
555             else
556                 budget = picture_control_set_ptr->sb_total_count * U_112;
557         }
558         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
559             if (picture_control_set_ptr->temporal_layer_index == 0)
560                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * FULL_SEARCH_COST;
561             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
562                 budget = picture_control_set_ptr->sb_total_count * U_130;
563             else
564                 budget = picture_control_set_ptr->sb_total_count * U_120;
565         }
566         else {
567             if (picture_control_set_ptr->temporal_layer_index == 0)
568                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * FULL_SEARCH_COST;
569             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
570                 budget = picture_control_set_ptr->sb_total_count * U_125;
571             else
572                 budget = picture_control_set_ptr->sb_total_count * U_115;
573 
574         }
575 
576     }
577     else if (context_ptr->adp_level == ENC_MODE_7) { // Adam
578         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_576p_RANGE_OR_LOWER) {
579             if (picture_control_set_ptr->temporal_layer_index == 0)
580                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
581             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
582                 budget = picture_control_set_ptr->sb_total_count * U_133;
583             else
584                 budget = picture_control_set_ptr->sb_total_count * U_120;
585         }
586         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
587             if (picture_control_set_ptr->temporal_layer_index == 0)
588                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
589             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
590                 budget = picture_control_set_ptr->sb_total_count * U_113;
591             else
592                 budget = picture_control_set_ptr->sb_total_count * U_112;
593         }
594         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
595             if (picture_control_set_ptr->temporal_layer_index == 0)
596                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * FULL_SEARCH_COST;
597             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
598                 budget = picture_control_set_ptr->sb_total_count * U_130;
599             else
600                 budget = picture_control_set_ptr->sb_total_count * U_120;
601         }
602         else {
603             if (picture_control_set_ptr->temporal_layer_index == 0)
604                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * FULL_SEARCH_COST;
605             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
606                 budget = picture_control_set_ptr->sb_total_count * U_125;
607             else
608                 budget = picture_control_set_ptr->sb_total_count * U_115;
609 
610         }
611     }
612     else if (context_ptr->adp_level == ENC_MODE_8) {
613         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_576p_RANGE_OR_LOWER) {
614             if (picture_control_set_ptr->temporal_layer_index == 0)
615                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
616             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
617                 budget = picture_control_set_ptr->sb_total_count * U_115;
618             else
619                 budget = picture_control_set_ptr->sb_total_count * U_114;
620         }
621         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
622             if (picture_control_set_ptr->temporal_layer_index == 0)
623                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
624             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
625                 budget = picture_control_set_ptr->sb_total_count * U_112;
626             else
627                 budget = picture_control_set_ptr->sb_total_count * U_111;
628         }
629         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
630             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
631                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST;
632             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
633                 budget = picture_control_set_ptr->sb_total_count * U_116;
634             else
635                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
636         }
637         else {
638             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
639                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * AVC_COST;
640             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
641                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * LIGHT_BDP_COST;
642             else
643                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_111;
644         }
645     }
646     else if (context_ptr->adp_level == ENC_MODE_9) {
647         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_576p_RANGE_OR_LOWER) {
648             if (picture_control_set_ptr->temporal_layer_index == 0)
649                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
650             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
651                 budget = picture_control_set_ptr->sb_total_count * U_112;
652             else
653                 budget = picture_control_set_ptr->sb_total_count * U_111;
654         }
655         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
656             if (picture_control_set_ptr->temporal_layer_index == 0)
657                 budget = picture_control_set_ptr->sb_total_count * U_127;
658             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
659                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
660             else
661                 budget = picture_control_set_ptr->sb_total_count * U_101;
662         }
663         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
664             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
665                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST;
666             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
667                 budget = picture_control_set_ptr->sb_total_count * U_113;
668             else
669                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
670         }
671         else {
672             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
673                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127;
674             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
675                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_114;
676             else
677                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST;
678 
679         }
680     }
681     else if (context_ptr->adp_level == ENC_MODE_10) {
682         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
683             if (picture_control_set_ptr->temporal_layer_index == 0)
684                 budget = picture_control_set_ptr->sb_total_count * U_127;
685             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
686                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
687             else
688                 budget = picture_control_set_ptr->sb_total_count * U_101;
689         }
690         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
691             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
692                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST;
693             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
694                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
695             else
696                 budget = picture_control_set_ptr->sb_total_count * U_104;
697         }
698         else {
699             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
700                 budget = (context_ptr->adp_depth_sensitive_picture_class) ?
701                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
702                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125;
703             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
704                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST;
705             else
706                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * LIGHT_OPEN_LOOP_COST;
707 
708         }
709     }
710     else if (context_ptr->adp_level == ENC_MODE_11) {
711         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
712             if (picture_control_set_ptr->temporal_layer_index == 0)
713                 budget = picture_control_set_ptr->sb_total_count * U_127;
714             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
715                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
716             else
717                 budget = picture_control_set_ptr->sb_total_count * U_101;
718         }
719         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
720 
721             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
722                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * BDP_COST;
723             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
724                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
725             else
726                 budget = picture_control_set_ptr->sb_total_count * U_104;
727 
728         }
729         else {
730             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
731                 budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
732                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
733                 (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
734                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125 :
735                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_121;
736             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
737                 budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
738                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST :
739                 (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
740                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_104 :
741                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_103;
742             else
743                 budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
744                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_104 :
745                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_103;
746         }
747     }
748     else {
749         if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
750             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
751             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
752             (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
753             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125 :
754             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_121;
755         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
756             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
757             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST :
758             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST ;
759         else
760             budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST;
761     }
762     context_ptr->budget = budget;
763 }
764 
765 /******************************************************
766 * Set the target budget
767 Input   : cost per depth
768 Output  : budget per picture
769 ******************************************************/
770 
eb_vp9_set_target_budget_oq(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)771 void eb_vp9_set_target_budget_oq(
772     SequenceControlSet               *sequence_control_set_ptr,
773     PictureControlSet                *picture_control_set_ptr,
774     ModeDecisionConfigurationContext *context_ptr)
775 {
776     uint32_t budget;
777 
778     if (context_ptr->adp_level <= ENC_MODE_3) {
779         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
780             if (picture_control_set_ptr->temporal_layer_index == 0)
781                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
782             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
783                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
784                 picture_control_set_ptr->sb_total_count * U_152 :
785                 picture_control_set_ptr->sb_total_count * U_150;
786             else
787                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
788                 picture_control_set_ptr->sb_total_count * U_152 :
789                 picture_control_set_ptr->sb_total_count * U_145;
790         }
791         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
792             if (picture_control_set_ptr->temporal_layer_index == 0)
793                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
794             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
795                 budget = picture_control_set_ptr->sb_total_count * AVC_COST;
796             else
797                 budget = picture_control_set_ptr->sb_total_count * U_134;
798         }
799         else {
800             if (picture_control_set_ptr->temporal_layer_index == 0)
801                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
802             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
803                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
804             else
805                 budget = picture_control_set_ptr->sb_total_count * U_109;
806         }
807     }
808     else if (context_ptr->adp_level <= ENC_MODE_4) {
809         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE) {
810             if (picture_control_set_ptr->temporal_layer_index == 0)
811                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
812             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
813                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
814                 picture_control_set_ptr->sb_total_count * U_152 :
815                 picture_control_set_ptr->sb_total_count * U_150;
816             else
817                 budget = (picture_control_set_ptr->parent_pcs_ptr->is_pan || picture_control_set_ptr->parent_pcs_ptr->is_tilt) ?
818                 picture_control_set_ptr->sb_total_count * U_152 :
819                 picture_control_set_ptr->sb_total_count * U_145;
820         }
821         else if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080p_RANGE) {
822             if (picture_control_set_ptr->temporal_layer_index == 0)
823                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
824             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
825                 budget = picture_control_set_ptr->sb_total_count * AVC_COST;
826             else
827                 budget = picture_control_set_ptr->sb_total_count * U_134;
828         }
829         else {
830             if (picture_control_set_ptr->temporal_layer_index == 0)
831                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
832             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
833                 budget = picture_control_set_ptr->sb_total_count * U_125;
834             else
835                 budget = picture_control_set_ptr->sb_total_count * U_121;
836         }
837     }
838     else if (context_ptr->adp_level <= ENC_MODE_5) {
839         if (picture_control_set_ptr->temporal_layer_index == 0)
840             budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
841         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
842             budget = picture_control_set_ptr->sb_total_count * AVC_COST;
843         else
844             budget = picture_control_set_ptr->sb_total_count * U_109;
845     }
846     else if (context_ptr->adp_level <= ENC_MODE_6) {
847         if (picture_control_set_ptr->temporal_layer_index == 0)
848             budget = picture_control_set_ptr->sb_total_count * BDP_COST;
849         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
850             budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
851         else
852             budget = picture_control_set_ptr->sb_total_count * U_109;
853     }
854     else if (context_ptr->adp_level <= ENC_MODE_7) {
855         if (sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE) {
856             if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
857                 budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
858                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
859                 (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
860                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125 :
861                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_121;
862             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
863                 budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
864                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST :
865                 picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST ;
866             else
867                 budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST;
868         }
869         else {
870             if (picture_control_set_ptr->temporal_layer_index == 0)
871                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
872             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
873                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
874             else
875                 budget = picture_control_set_ptr->sb_total_count * U_109;
876         }
877     }
878     else {
879         if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
880             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
881             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
882             (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
883             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125 :
884             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_121 ;
885         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
886             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
887             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST :
888             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST;
889         else
890             budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST;
891     }
892 
893     context_ptr->budget = budget;
894 }
895 
896 /******************************************************
897 * Set the target budget
898 Input   : cost per depth
899 Output  : budget per picture
900 ******************************************************/
901 
set_target_budget_vmaf(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)902 void set_target_budget_vmaf(
903     SequenceControlSet               *sequence_control_set_ptr,
904     PictureControlSet                *picture_control_set_ptr,
905     ModeDecisionConfigurationContext *context_ptr)
906 {
907     uint32_t budget;
908 
909     if (context_ptr->adp_level <= ENC_MODE_3) {
910         if (picture_control_set_ptr->temporal_layer_index == 0)
911             budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
912         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
913             budget = picture_control_set_ptr->sb_total_count * AVC_COST;
914         else
915             budget = picture_control_set_ptr->sb_total_count * U_134;
916     }
917     else if (context_ptr->adp_level <= ENC_MODE_5) {
918         if (sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE) {
919             if (picture_control_set_ptr->temporal_layer_index == 0)
920                 budget = picture_control_set_ptr->sb_total_count * BDP_COST;
921             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
922                 budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
923             else
924                 budget = picture_control_set_ptr->sb_total_count * U_109;
925         }
926         else {
927             if (picture_control_set_ptr->temporal_layer_index == 0)
928                 budget = picture_control_set_ptr->sb_total_count * FULL_SEARCH_COST;
929             else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
930                 budget = picture_control_set_ptr->sb_total_count * AVC_COST;
931             else
932                 budget = picture_control_set_ptr->sb_total_count * U_134;
933         }
934     }
935     else if (context_ptr->adp_level <= ENC_MODE_8) {
936         if (picture_control_set_ptr->temporal_layer_index == 0)
937             budget = picture_control_set_ptr->sb_total_count * BDP_COST;
938         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE)
939             budget = picture_control_set_ptr->sb_total_count * OPEN_LOOP_COST;
940         else
941             budget = picture_control_set_ptr->sb_total_count * U_109;
942     }
943     else {
944         if (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0)
945             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
946             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_127 :
947             (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_1) ?
948             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_125 :
949             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * U_121;
950         else if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag)
951             budget = (context_ptr->adp_depth_sensitive_picture_class == DEPTH_SENSITIVE_PIC_CLASS_2) ?
952             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * OPEN_LOOP_COST :
953             picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST ;
954         else
955             budget = picture_control_set_ptr->parent_pcs_ptr->sb_total_count * PRED_OPEN_LOOP_COST;
956     }
957 
958     context_ptr->budget = budget;
959 }
960 
961 /******************************************************
962  * is_avc_partitioning_mode()
963  * Returns TRUE for LCUs where only Depth2 & Depth3
964  * (AVC Partitioning) are goind to be tested by MD
965  * The LCU is marked if Sharpe Edge or Potential Aura/Grass
966  * or B-Logo or S-Logo or Potential Blockiness Area
967  * Input: Sharpe Edge, Potential Aura/Grass, B-Logo, S-Logo, Potential Blockiness Area signals
968  * Output: TRUE if one of the above is TRUE
969  ******************************************************/
is_avc_partitioning_mode(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,SbUnit * sb_ptr)970 EB_BOOL is_avc_partitioning_mode(
971     SequenceControlSet *sequence_control_set_ptr,
972     PictureControlSet  *picture_control_set_ptr,
973     SbUnit             *sb_ptr)
974 {
975     const uint32_t sb_index = sb_ptr->sb_index;
976     SbParams      *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
977     EB_SLICE       slice_type = picture_control_set_ptr->slice_type;
978     uint8_t        edge_block_num = picture_control_set_ptr->parent_pcs_ptr->edge_results_ptr[sb_index].edge_block_num;
979     SbStat        *sb_stat_ptr = &(picture_control_set_ptr->parent_pcs_ptr->sb_stat_array[sb_index]);
980     uint8_t        stationary_edge_over_time_flag = sb_stat_ptr->stationary_edge_over_time_flag;
981     uint8_t        aura_status = sb_ptr->aura_status;
982 
983     if (sequence_control_set_ptr->static_config.tune == TUNE_SQ) {
984         // No refinment for sub-1080p
985         if (sequence_control_set_ptr->input_resolution <= INPUT_SIZE_1080i_RANGE)
986             return EB_FALSE;
987 
988         // Sharpe Edge
989         if (picture_control_set_ptr->parent_pcs_ptr->high_dark_low_light_area_density_flag && picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index > 0 && picture_control_set_ptr->parent_pcs_ptr->sharp_edge_sb_flag[sb_index] && !picture_control_set_ptr->parent_pcs_ptr->similar_colocated_sb_array_all_layers[sb_index]) {
990             return EB_TRUE;
991         }
992 
993         // Potential Aura/Grass
994         if (picture_control_set_ptr->scene_characteristic_id == EB_FRAME_CARAC_0) {
995             if (picture_control_set_ptr->parent_pcs_ptr->grass_percentage_in_picture > 60 && aura_status == AURA_STATUS_1) {
996                 if (slice_type != I_SLICE && sb_params->is_complete_sb) {
997                     return EB_TRUE;
998                 }
999             }
1000         }
1001 
1002         // B-Logo
1003         if (picture_control_set_ptr->parent_pcs_ptr->logo_pic_flag && edge_block_num)
1004             return EB_TRUE;
1005 
1006         // S-Logo
1007         if (stationary_edge_over_time_flag > 0)
1008             return EB_TRUE;
1009 
1010         // Potential Blockiness Area
1011         if (picture_control_set_ptr->parent_pcs_ptr->complex_sb_array[sb_index] == SB_COMPLEXITY_STATUS_2)
1012             return EB_TRUE;
1013     }
1014 
1015     return EB_FALSE;
1016 }
1017 
1018 /******************************************************
1019 * Load the cost of the different partitioning method into a local array and derive sensitive picture flag
1020     Input   : the offline derived cost per search method, detection signals
1021     Output  : valid cost_depth_mode and valid sensitivePicture
1022 ******************************************************/
eb_vp9_configure_adp(PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1023 void eb_vp9_configure_adp(
1024     PictureControlSet                *picture_control_set_ptr,
1025     ModeDecisionConfigurationContext *context_ptr)
1026 {
1027 
1028     context_ptr->cost_depth_mode[SB_FULL85_DEPTH_MODE - 1]               = FULL_SEARCH_COST;
1029     context_ptr->cost_depth_mode[SB_FULL84_DEPTH_MODE - 1]               = FULL_SEARCH_COST;
1030     context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1]                  = BDP_COST;
1031     context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1]            = LIGHT_BDP_COST;
1032     context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1]            = OPEN_LOOP_COST;
1033     context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1]      = LIGHT_OPEN_LOOP_COST;
1034     context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1]                  = AVC_COST;
1035     context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1]            = LIGHT_AVC_COST;
1036     context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1]       = PRED_OPEN_LOOP_COST;
1037     context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE - 1] = PRED_OPEN_LOOP_1_NFL_COST;
1038 
1039     // Initialize the score based TH
1040     context_ptr->score_th[0] = ~0;
1041     context_ptr->score_th[1] = ~0;
1042     context_ptr->score_th[2] = ~0;
1043     context_ptr->score_th[3] = ~0;
1044     context_ptr->score_th[4] = ~0;
1045     context_ptr->score_th[5] = ~0;
1046     context_ptr->score_th[6] = ~0;
1047 
1048     // Initialize the predicted budget
1049     context_ptr->predicted_cost = (uint32_t) ~0;
1050 
1051     // Initialize the predicted budget
1052     context_ptr->predicted_cost = (uint32_t)~0;
1053 
1054     // Derive the sensitive picture flag
1055     context_ptr->adp_depth_sensitive_picture_class = DEPTH_SENSITIVE_PIC_CLASS_0;
1056 
1057     EB_BOOL luminosity_change = EB_FALSE;
1058     // Derived for REF P & B & kept false otherwise (for temporal distance equal to 1 luminosity changes are easier to handle)
1059     // Derived for P & B
1060     if (picture_control_set_ptr->slice_type != I_SLICE) {
1061         if (picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag) {
1062             EbReferenceObject    * ref_obj_l0, *ref_obj_l1;
1063             ref_obj_l0 = (EbReferenceObject  *)picture_control_set_ptr->ref_pic_ptr_array[REF_LIST_0]->object_ptr;
1064             ref_obj_l1 = (picture_control_set_ptr->parent_pcs_ptr->slice_type == B_SLICE) ? (EbReferenceObject  *)picture_control_set_ptr->ref_pic_ptr_array[REF_LIST_1]->object_ptr : (EbReferenceObject  *)EB_NULL;
1065             luminosity_change = ((ABS(picture_control_set_ptr->parent_pcs_ptr->average_intensity[0] - ref_obj_l0->average_intensity) >= adp_luminosity_change_th_array[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->temporal_layer_index]) || (ref_obj_l1 != EB_NULL && ABS(picture_control_set_ptr->parent_pcs_ptr->average_intensity[0] - ref_obj_l1->average_intensity) >= adp_luminosity_change_th_array[picture_control_set_ptr->parent_pcs_ptr->hierarchical_levels][picture_control_set_ptr->temporal_layer_index]));
1066         }
1067     }
1068 
1069     if (picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score != INVALID_NON_MOVING_SCORE && picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score < ADP_CONFIG_NON_MOVING_INDEX_TH_1) { // could not seen by the eye if very active
1070         if (picture_control_set_ptr->parent_pcs_ptr->pic_noise_class > PIC_NOISE_CLASS_3 || picture_control_set_ptr->parent_pcs_ptr->high_dark_low_light_area_density_flag ||luminosity_change) { // potential complex picture: luminosity Change (e.g. fade, light..)
1071             context_ptr->adp_depth_sensitive_picture_class = DEPTH_SENSITIVE_PIC_CLASS_2;
1072         }
1073         // potential complex picture: light foreground and dark background(e.g.flash, light..) or moderate activity and high variance (noise or a lot of edge)
1074         else if ( (picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score >= ADP_CONFIG_NON_MOVING_INDEX_TH_0 && picture_control_set_ptr->parent_pcs_ptr->pic_noise_class == PIC_NOISE_CLASS_3)) {
1075             context_ptr->adp_depth_sensitive_picture_class = DEPTH_SENSITIVE_PIC_CLASS_1;
1076         }
1077     }
1078 
1079 }
1080 
1081 /******************************************************
1082 * Assign a search method based on the allocated cost
1083     Input   : allocated budget per LCU
1084     Output  : search method per LCU
1085 ******************************************************/
eb_vp9_derive_search_method(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1086 void eb_vp9_derive_search_method(
1087     SequenceControlSet               *sequence_control_set_ptr,
1088     PictureControlSet                *picture_control_set_ptr,
1089     ModeDecisionConfigurationContext *context_ptr)
1090 {
1091 
1092     uint32_t sb_index;
1093 
1094     picture_control_set_ptr->bdp_present_flag = EB_FALSE;
1095     picture_control_set_ptr->md_present_flag  = EB_FALSE;
1096 
1097     for (sb_index = 0; sb_index < picture_control_set_ptr->parent_pcs_ptr->sb_total_count; sb_index++) {
1098 
1099         if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE - 1]) {
1100             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE;
1101             picture_control_set_ptr->md_present_flag = EB_TRUE;
1102         }
1103         else
1104         if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_PRED_OPEN_LOOP_DEPTH_MODE - 1]) {
1105             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_PRED_OPEN_LOOP_DEPTH_MODE;
1106             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1107         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_LIGHT_OPEN_LOOP_DEPTH_MODE - 1]) {
1108             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_LIGHT_OPEN_LOOP_DEPTH_MODE;
1109             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1110         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_OPEN_LOOP_DEPTH_MODE - 1]) {
1111             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_OPEN_LOOP_DEPTH_MODE;
1112             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1113         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_LIGHT_BDP_DEPTH_MODE - 1]) {
1114             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_LIGHT_BDP_DEPTH_MODE;
1115             picture_control_set_ptr->bdp_present_flag = EB_TRUE;
1116         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_BDP_DEPTH_MODE - 1]) {
1117             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_BDP_DEPTH_MODE;
1118             picture_control_set_ptr->bdp_present_flag = EB_TRUE;
1119         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1]) {
1120             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_AVC_DEPTH_MODE;
1121             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1122         } else if (context_ptr->sb_cost_array[sb_index] == context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1]) {
1123             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_LIGHT_AVC_DEPTH_MODE;
1124             picture_control_set_ptr->md_present_flag = EB_TRUE;
1125 #if SHUT_64x64_BASE_RESTRICTION
1126         } else if (picture_control_set_ptr->temporal_layer_index == 0 && (sequence_control_set_ptr->static_config.tune == TUNE_SQ || sequence_control_set_ptr->static_config.tune == TUNE_VMAF)) {
1127             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_FULL84_DEPTH_MODE;
1128             picture_control_set_ptr->md_present_flag = EB_TRUE;
1129 #else
1130         } else if (picture_control_set_ptr->temporal_layer_index == 0) {
1131             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_FULL84_DEPTH_MODE;
1132             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1133 #endif
1134         } else {
1135             picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] = SB_FULL85_DEPTH_MODE;
1136             picture_control_set_ptr->md_present_flag  = EB_TRUE;
1137         }
1138 
1139     }
1140 
1141 #if ADP_STATS_PER_LAYER
1142     SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_wrapper_ptr->object_ptr;
1143 
1144     for (sb_index = 0; sb_index < picture_control_set_ptr->parent_pcs_ptr->sb_total_count; sb_index++) {
1145 
1146         sequence_control_set_ptr->total_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index] ++;
1147 
1148         if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_FULL85_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_FULL84_DEPTH_MODE) {
1149             sequence_control_set_ptr->fs_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1150         }
1151         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_BDP_DEPTH_MODE) {
1152             sequence_control_set_ptr->f_bdp_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1153         }
1154         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_LIGHT_BDP_DEPTH_MODE) {
1155             sequence_control_set_ptr->l_bdp_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1156         }
1157         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_OPEN_LOOP_DEPTH_MODE) {
1158             sequence_control_set_ptr->f_mdc_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1159         }
1160         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_LIGHT_OPEN_LOOP_DEPTH_MODE) {
1161             sequence_control_set_ptr->l_mdc_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1162         }
1163         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_AVC_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_LIGHT_AVC_DEPTH_MODE) {
1164             sequence_control_set_ptr->avc_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1165         }
1166         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_DEPTH_MODE) {
1167             sequence_control_set_ptr->pred_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1168         }
1169         else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE) {
1170             sequence_control_set_ptr->pred1_nfl_count[picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index]  ++;
1171         }
1172         else
1173         {
1174             SVT_LOG("error");
1175         }
1176     }
1177 #endif
1178 
1179 }
1180 
1181 /******************************************************
1182 * Set LCU budget
1183     Input   : LCU score, detection signals, iteration
1184     Output  : predicted budget for the LCU
1185 ******************************************************/
eb_vp9_set_sb_budget(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,SbUnit * sb_ptr,ModeDecisionConfigurationContext * context_ptr)1186 void eb_vp9_set_sb_budget(
1187     SequenceControlSet               *sequence_control_set_ptr,
1188     PictureControlSet                *picture_control_set_ptr,
1189     SbUnit                           *sb_ptr,
1190     ModeDecisionConfigurationContext *context_ptr)
1191 {
1192     const uint32_t      sb_index = sb_ptr->sb_index;
1193     uint32_t      max_to_min_score, score_to_min;
1194 
1195     const EB_BOOL is_avc_partitioning_mode_flag = is_avc_partitioning_mode(sequence_control_set_ptr, picture_control_set_ptr, sb_ptr);
1196 
1197     if (context_ptr->adp_refinement_mode == 2 && is_avc_partitioning_mode_flag) {
1198 
1199         context_ptr->sb_cost_array[sb_index] = context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1];
1200         context_ptr->predicted_cost += context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1];
1201 
1202     }
1203     else if (context_ptr->adp_refinement_mode == 1 && is_avc_partitioning_mode_flag) {
1204 
1205         context_ptr->sb_cost_array[sb_index] = context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1];
1206         context_ptr->predicted_cost += context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1];
1207 
1208     }
1209     else {
1210         context_ptr->sb_score_array[sb_index] = CLIP3(context_ptr->sb_min_score, context_ptr->sb_max_score, context_ptr->sb_score_array[sb_index]);
1211         score_to_min = context_ptr->sb_score_array[sb_index] - context_ptr->sb_min_score;
1212         max_to_min_score = context_ptr->sb_max_score - context_ptr->sb_min_score;
1213 
1214         if ((score_to_min <= (max_to_min_score * context_ptr->score_th[0]) / 100 && context_ptr->score_th[0] != 0) || context_ptr->number_of_segments == 1 || context_ptr->score_th[1] == 100) {
1215             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[0];
1216             context_ptr->predicted_cost += context_ptr->interval_cost[0];
1217         }
1218         else if ((score_to_min <= (max_to_min_score * context_ptr->score_th[1]) / 100 && context_ptr->score_th[1] != 0) || context_ptr->number_of_segments == 2 || context_ptr->score_th[2] == 100) {
1219             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[1];
1220             context_ptr->predicted_cost += context_ptr->interval_cost[1];
1221         }
1222         else if ((score_to_min <= (max_to_min_score * context_ptr->score_th[2]) / 100 && context_ptr->score_th[2] != 0) || context_ptr->number_of_segments == 3 || context_ptr->score_th[3] == 100) {
1223             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[2];
1224             context_ptr->predicted_cost += context_ptr->interval_cost[2];
1225         }
1226         else if ((score_to_min <= (max_to_min_score * context_ptr->score_th[3]) / 100 && context_ptr->score_th[3] != 0) || context_ptr->number_of_segments == 4 || context_ptr->score_th[4] == 100) {
1227             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[3];
1228             context_ptr->predicted_cost += context_ptr->interval_cost[3];
1229         }
1230         else if ((score_to_min <= (max_to_min_score * context_ptr->score_th[4]) / 100 && context_ptr->score_th[4] != 0) || context_ptr->number_of_segments == 5 || context_ptr->score_th[5] == 100) {
1231             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[4];
1232             context_ptr->predicted_cost += context_ptr->interval_cost[4];
1233         }
1234         else if ((score_to_min <= (max_to_min_score * context_ptr->score_th[5]) / 100 && context_ptr->score_th[5] != 0) || context_ptr->number_of_segments == 6 || context_ptr->score_th[6] == 100) {
1235             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[5];
1236             context_ptr->predicted_cost += context_ptr->interval_cost[5];
1237         }
1238         else {
1239             context_ptr->sb_cost_array[sb_index] = context_ptr->interval_cost[6];
1240             context_ptr->predicted_cost += context_ptr->interval_cost[6];
1241         }
1242         // Switch to AVC mode if the LCU cost is higher than the AVC cost and the the LCU is marked + adjust the current picture cost accordingly
1243         if (is_avc_partitioning_mode_flag) {
1244             if (context_ptr->sb_cost_array[sb_index] > context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1]) {
1245                 context_ptr->predicted_cost -= (context_ptr->sb_cost_array[sb_index] - context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1]);
1246                 context_ptr->sb_cost_array[sb_index] = context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1];
1247             }
1248             else if (context_ptr->sb_cost_array[sb_index] > context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1] && picture_control_set_ptr->temporal_layer_index > 0) {
1249                 context_ptr->predicted_cost -= (context_ptr->sb_cost_array[sb_index] - context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1]);
1250                 context_ptr->sb_cost_array[sb_index] = context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1];
1251             }
1252         }
1253     }
1254 }
1255 
1256 /******************************************************
1257 * Loop multiple times over the LCUs in order to derive the optimal budget per LCU
1258     Input   : budget per picture, ditortion, detection signals, iteration
1259     Output  : optimal budget for each LCU
1260 ******************************************************/
eb_vp9_derive_optimal_budget_per_sb(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1261 void  eb_vp9_derive_optimal_budget_per_sb(
1262     SequenceControlSet               *sequence_control_set_ptr,
1263     PictureControlSet                *picture_control_set_ptr,
1264     ModeDecisionConfigurationContext *context_ptr)
1265 {
1266     uint32_t sb_index;
1267     // Initialize the deviation between the picture predicted cost & the target budget to 100,
1268     uint32_t deviation_to_target    = 1000;
1269 
1270     // Set the adjustment step to 1 (could be increased for faster convergence),
1271     int8_t  adjustement_step      =  1;
1272 
1273     // Set the initial shooting state & the final shooting state to TBD
1274     uint32_t initial_shooting  = TBD_SHOOTING;
1275     uint32_t final_shooting    = TBD_SHOOTING;
1276 
1277     uint8_t max_adjustement_iteration   = 100;
1278     uint8_t adjustement_iteration      =   0;
1279 
1280     while (deviation_to_target != 0 && (initial_shooting == final_shooting) && adjustement_iteration <= max_adjustement_iteration) {
1281 
1282         if (context_ptr->predicted_cost < context_ptr->budget) {
1283             initial_shooting =    UNDER_SHOOTING;
1284         }
1285         else {
1286             initial_shooting =   OVER_SHOOTING;
1287         }
1288 
1289         // reset running cost
1290         context_ptr->predicted_cost = 0;
1291 
1292         for (sb_index = 0; sb_index < picture_control_set_ptr->parent_pcs_ptr->sb_total_count; sb_index++) {
1293 
1294             SbUnit* sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
1295 
1296             eb_vp9_set_sb_budget(
1297                 sequence_control_set_ptr,
1298                 picture_control_set_ptr,
1299                 sb_ptr,
1300                 context_ptr);
1301         }
1302 
1303         // Compute the deviation between the predicted budget & the target budget
1304         deviation_to_target = (ABS((int32_t)(context_ptr->predicted_cost - context_ptr->budget)) * 1000) / context_ptr->budget;
1305         // Derive shooting status
1306         if (context_ptr->predicted_cost < context_ptr->budget) {
1307             context_ptr->score_th[0] = MAX((context_ptr->score_th[0] - adjustement_step), 0);
1308             context_ptr->score_th[1] = MAX((context_ptr->score_th[1] - adjustement_step), 0);
1309             context_ptr->score_th[2] = MAX((context_ptr->score_th[2] - adjustement_step), 0);
1310             context_ptr->score_th[3] = MAX((context_ptr->score_th[3] - adjustement_step), 0);
1311             context_ptr->score_th[4] = MAX((context_ptr->score_th[4] - adjustement_step), 0);
1312             final_shooting = UNDER_SHOOTING;
1313         }
1314         else {
1315             context_ptr->score_th[0] = (context_ptr->score_th[0] == 0) ? 0 : MIN(context_ptr->score_th[0] + adjustement_step, 100);
1316             context_ptr->score_th[1] = (context_ptr->score_th[1] == 0) ? 0 : MIN(context_ptr->score_th[1] + adjustement_step, 100);
1317             context_ptr->score_th[2] = (context_ptr->score_th[2] == 0) ? 0 : MIN(context_ptr->score_th[2] + adjustement_step, 100);
1318             context_ptr->score_th[3] = (context_ptr->score_th[3] == 0) ? 0 : MIN(context_ptr->score_th[3] + adjustement_step, 100);
1319             context_ptr->score_th[4] = (context_ptr->score_th[4] == 0) ? 0 : MIN(context_ptr->score_th[4] + adjustement_step, 100);
1320             final_shooting = OVER_SHOOTING;
1321         }
1322 
1323         if (adjustement_iteration == 0)
1324             initial_shooting = final_shooting ;
1325 
1326         adjustement_iteration ++;
1327     }
1328 }
1329 
1330 /******************************************************
1331 * Compute the refinment cost
1332     Input   : budget per picture, and the cost of the refinment
1333     Output  : the refinment flag
1334 ******************************************************/
compute_refinement_cost(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1335 void compute_refinement_cost(
1336     SequenceControlSet               *sequence_control_set_ptr,
1337     PictureControlSet                *picture_control_set_ptr,
1338     ModeDecisionConfigurationContext *context_ptr)
1339 {
1340 
1341     uint32_t  sb_index;
1342     uint32_t  avc_refinement_cost = 0;
1343     uint32_t  light_avc_refinement_cost = 0;
1344 
1345     for (sb_index = 0; sb_index < picture_control_set_ptr->parent_pcs_ptr->sb_total_count; sb_index++) {
1346         SbUnit* sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
1347         if (is_avc_partitioning_mode(sequence_control_set_ptr, picture_control_set_ptr, sb_ptr)) {
1348             avc_refinement_cost       += context_ptr->cost_depth_mode[SB_AVC_DEPTH_MODE - 1];
1349             light_avc_refinement_cost  += context_ptr->cost_depth_mode[SB_LIGHT_AVC_DEPTH_MODE - 1];
1350 
1351         }
1352         // assumes the fastest mode will be used otherwise
1353         else {
1354             avc_refinement_cost       += context_ptr->interval_cost[0];
1355             light_avc_refinement_cost  += context_ptr->interval_cost[0];
1356         }
1357     }
1358 
1359     // Shut the refinement if the refinement cost is higher than the allocated budget
1360     if (avc_refinement_cost <= context_ptr->budget && ((context_ptr->budget > (uint32_t)(LIGHT_BDP_COST * picture_control_set_ptr->parent_pcs_ptr->sb_total_count)) || picture_control_set_ptr->temporal_layer_index == 0 || (sequence_control_set_ptr->input_resolution < INPUT_SIZE_4K_RANGE && picture_control_set_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE))) {
1361         context_ptr->adp_refinement_mode = ADP_MODE_1;
1362     }
1363     else if (light_avc_refinement_cost <= context_ptr->budget && picture_control_set_ptr->temporal_layer_index > 0) {
1364         context_ptr->adp_refinement_mode = ADP_MODE_0;
1365     }
1366     else {
1367         context_ptr->adp_refinement_mode = ADP_REFINEMENT_OFF;
1368     }
1369 
1370 }
1371 /******************************************************
1372 * Compute the score of each LCU
1373     Input   : distortion, detection signals
1374     Output  : LCU score
1375 ******************************************************/
eb_vp9_derive_sb_score(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1376 void eb_vp9_derive_sb_score(
1377     SequenceControlSet               *sequence_control_set_ptr,
1378     PictureControlSet                *picture_control_set_ptr,
1379     ModeDecisionConfigurationContext *context_ptr)
1380 {
1381     uint32_t  sb_index;
1382     uint32_t  sb_score = 0;
1383     uint32_t  distortion;
1384 
1385     context_ptr->sb_min_score = ~0u;
1386     context_ptr->sb_max_score = 0u;
1387 
1388     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; sb_index++) {
1389 
1390         SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
1391 
1392         if (picture_control_set_ptr->slice_type == I_SLICE) {
1393             assert(0);
1394         }
1395         else {
1396             if (sb_params->pa_raster_scan_block_validity[PA_RASTER_SCAN_CU_INDEX_64x64] == EB_FALSE) {
1397 
1398                 uint8_t cu8x8Index;
1399                 uint8_t validCu8x8Count = 0;
1400                 distortion = 0;
1401                 for (cu8x8Index = PA_RASTER_SCAN_CU_INDEX_8x8_0; cu8x8Index <= PA_RASTER_SCAN_CU_INDEX_8x8_63; cu8x8Index++) {
1402                     if (sb_params->pa_raster_scan_block_validity[cu8x8Index]) {
1403                         distortion += picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index][cu8x8Index].distortion_direction[0].distortion;
1404                         validCu8x8Count++;
1405                     }
1406                 }
1407                 if (validCu8x8Count > 0)
1408                     distortion = CLIP3(picture_control_set_ptr->parent_pcs_ptr->min_me_distortion, picture_control_set_ptr->parent_pcs_ptr->max_me_distortion, (distortion / validCu8x8Count) * 64);
1409 
1410                 // Do not perform LCU score manipulation for incomplete LCUs as not valid signals
1411                 sb_score = distortion;
1412 
1413             }
1414             else {
1415                 distortion = picture_control_set_ptr->parent_pcs_ptr->me_results[sb_index][PA_RASTER_SCAN_CU_INDEX_64x64].distortion_direction[0].distortion;
1416 
1417                 sb_score = distortion;
1418 
1419                 if (sequence_control_set_ptr->static_config.tune == TUNE_SQ) {
1420 
1421                     if (
1422                         // LCU @ a picture boundary
1423                         sb_params->is_edge_sb
1424                         && picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index] != INVALID_NON_MOVING_SCORE
1425                         && picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score != INVALID_NON_MOVING_SCORE
1426                         // Active LCU
1427                         && picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index] >= ADP_CLASS_NON_MOVING_INDEX_TH_0
1428                         // Active Picture or LCU belongs to the most active LCUs
1429                         && (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index] >= picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score || picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score > ADP_CLASS_NON_MOVING_INDEX_TH_1)
1430                         // Off for sub-4K (causes instability as % of picture boundary LCUs is 2x higher for 1080p than for 4K (9% vs. 18% ) => might hurt the non-boundary LCUs)
1431                         && sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE) {
1432 
1433                         sb_score += (((picture_control_set_ptr->parent_pcs_ptr->max_me_distortion - sb_score) * ADP_CLASS_SHIFT_DIST_1) / 100);
1434 
1435                     }
1436                     else {
1437 
1438                         // Use LCU variance & activity
1439                         if (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index] == ADP_CLASS_NON_MOVING_INDEX_TH_2 && picture_control_set_ptr->parent_pcs_ptr->variance[sb_index][PA_RASTER_SCAN_CU_INDEX_64x64] > IS_COMPLEX_SB_VARIANCE_TH && (sequence_control_set_ptr->static_config.frame_rate >> 16) > 30)
1440 
1441                             sb_score -= (((sb_score - picture_control_set_ptr->parent_pcs_ptr->min_me_distortion) * ADP_CLASS_SHIFT_DIST_0) / 100);
1442                         // Use LCU luminosity
1443                         if (sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE) {
1444                             // Shift to the left dark LCUs & shift to the right otherwise ONLY if a high dark area is present
1445                             if (picture_control_set_ptr->parent_pcs_ptr->black_area_percentage > ADP_BLACK_AREA_PERCENTAGE) {
1446                                 if (picture_control_set_ptr->parent_pcs_ptr->y_mean[sb_index][PA_RASTER_SCAN_CU_INDEX_64x64] < ADP_DARK_SB_TH)
1447                                     sb_score -= (((sb_score - picture_control_set_ptr->parent_pcs_ptr->min_me_distortion) * ADP_CLASS_SHIFT_DIST_0) / 100);
1448                                 else
1449                                     sb_score += (((picture_control_set_ptr->parent_pcs_ptr->max_me_distortion - sb_score) * ADP_CLASS_SHIFT_DIST_0) / 100);
1450                             }
1451                         }
1452                         else {
1453                             // Shift to the left dark LCUs & shift to the right otherwise
1454                             if (picture_control_set_ptr->parent_pcs_ptr->y_mean[sb_index][PA_RASTER_SCAN_CU_INDEX_64x64] < ADP_DARK_SB_TH)
1455                                 sb_score -= (((sb_score - picture_control_set_ptr->parent_pcs_ptr->min_me_distortion) * ADP_CLASS_SHIFT_DIST_0) / 100);
1456                             else
1457                                 sb_score += (((picture_control_set_ptr->parent_pcs_ptr->max_me_distortion - sb_score) * ADP_CLASS_SHIFT_DIST_0) / 100);
1458                         }
1459 
1460                     }
1461                 }
1462             }
1463         }
1464 
1465         context_ptr->sb_score_array[sb_index] = sb_score;
1466 
1467         // Track MIN & MAX LCU scores
1468         context_ptr->sb_min_score = MIN(context_ptr->sb_score_array[sb_index], context_ptr->sb_min_score);
1469         context_ptr->sb_max_score = MAX(context_ptr->sb_score_array[sb_index], context_ptr->sb_max_score);
1470     }
1471 }
1472 
1473 /******************************************************
1474 * BudgetingOutlierRemovalLcu
1475     Input   : LCU score histogram
1476     Output  : Adjusted min & max LCU score (to be used to clip the LCU score @ eb_vp9_set_sb_budget)
1477  Performs outlier removal by:
1478  1. dividing the total distance between the maximum sb_score and the minimum sb_score into NI intervals(NI = 10).
1479  For each sb_score interval,
1480  2. computing the number of LCUs NV with sb_score belonging to the subject sb_score interval.
1481  3. marking the subject sb_score interval as not valid if its NV represent less than validity threshold V_TH per - cent of the total number of the processed LCUs in the picture. (V_TH = 2)
1482  4. computing the distance MIN_D from 0 and the index of the first, in incremental way, sb_score interval marked as valid in the prior step.
1483  5. computing the distance MAX_D from NI and the index of the first, in decreasing way, sb_score interval marked as valid in the prior step.
1484  6. adjusting the minimum and maximum sb_score as follows :
1485     MIN_SCORE = MIN_SCORE + MIN_D * I_Value.
1486     MAX_SCORE = MAX_SCORE - MAX_D * I_Value.
1487 ******************************************************/
1488 
perform_outlier_removal(SequenceControlSet * sequence_control_set_ptr,PictureParentControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1489 void perform_outlier_removal(
1490     SequenceControlSet               *sequence_control_set_ptr,
1491     PictureParentControlSet          *picture_control_set_ptr,
1492     ModeDecisionConfigurationContext *context_ptr)
1493 {
1494 
1495     uint32_t max_interval = 0;
1496     uint32_t sub_interval = 0;
1497     uint32_t sb_scoreHistogram[10] = { 0 };
1498     uint32_t sb_index;
1499     uint32_t sb_score;
1500     uint32_t processed_sb_count = 0;
1501     int32_t slot = 0;
1502 
1503     max_interval = context_ptr->sb_max_score - context_ptr->sb_min_score;
1504     // Consider 10 bins
1505     sub_interval = max_interval / 10;
1506 
1507     // Count # of LCUs at each bin
1508     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; sb_index++) {
1509 
1510         SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
1511 
1512         if (sb_params->pa_raster_scan_block_validity[PA_RASTER_SCAN_CU_INDEX_64x64]) {
1513 
1514             processed_sb_count++;
1515 
1516             sb_score = context_ptr->sb_score_array[sb_index] + context_ptr->sb_min_score;
1517             if (sb_score < (sub_interval + context_ptr->sb_min_score)){
1518                 sb_scoreHistogram[0]++;
1519             }
1520             else if (sb_score < ((2 * sub_interval) + context_ptr->sb_min_score)){
1521                 sb_scoreHistogram[1]++;
1522             }
1523             else if (sb_score < ((3 * sub_interval) + context_ptr->sb_min_score)){
1524                 sb_scoreHistogram[2]++;
1525             }
1526             else if (sb_score < ((4 * sub_interval) + context_ptr->sb_min_score)){
1527                 sb_scoreHistogram[3]++;
1528             }
1529             else if (sb_score < ((5 * sub_interval) + context_ptr->sb_min_score)){
1530                 sb_scoreHistogram[4]++;
1531             }
1532             else if (sb_score < ((6 * sub_interval) + context_ptr->sb_min_score)){
1533                 sb_scoreHistogram[5]++;
1534             }
1535             else if (sb_score < ((7 * sub_interval) + context_ptr->sb_min_score)){
1536                 sb_scoreHistogram[6]++;
1537             }
1538             else if (sb_score < ((8 * sub_interval) + context_ptr->sb_min_score)){
1539                 sb_scoreHistogram[7]++;
1540             }
1541             else if (sb_score < ((9 * sub_interval) + context_ptr->sb_min_score)){
1542                 sb_scoreHistogram[8]++;
1543             }
1544             else if (sb_score < ((10 * sub_interval) + context_ptr->sb_min_score)){
1545                 sb_scoreHistogram[9]++;
1546             }
1547         }
1548     }
1549 
1550     // Zero-out the bin if percentage lower than VALID_SLOT_TH
1551     for (slot = 0; slot < 10; slot++){
1552         if (processed_sb_count > 0 && (sb_scoreHistogram[slot] * 100 / processed_sb_count) < VALID_SLOT_TH){
1553             sb_scoreHistogram[slot] = 0;
1554         }
1555     }
1556 
1557     // Ignore null bins
1558     for (slot = 0; slot < 10; slot++){
1559         if (sb_scoreHistogram[slot]){
1560             context_ptr->sb_min_score = context_ptr->sb_min_score+ (slot * sub_interval);
1561             break;
1562         }
1563     }
1564 
1565     for (slot = 9; slot >= 0; slot--){
1566         if (sb_scoreHistogram[slot]){
1567             context_ptr->sb_max_score = context_ptr->sb_max_score - ((9 - slot) * sub_interval);
1568             break;
1569         }
1570     }
1571 }
1572 /******************************************************
1573 * Assign a search method for each LCU
1574     Input   : LCU score, detection signals
1575     Output  : search method for each LCU
1576 ******************************************************/
eb_vp9_derive_sb_md_mode(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1577 void eb_vp9_derive_sb_md_mode(
1578     SequenceControlSet               *sequence_control_set_ptr,
1579     PictureControlSet                *picture_control_set_ptr,
1580     ModeDecisionConfigurationContext *context_ptr) {
1581 
1582     // Configure ADP
1583     eb_vp9_configure_adp(
1584         picture_control_set_ptr,
1585         context_ptr);
1586 
1587     // Set the target budget
1588     if (sequence_control_set_ptr->static_config.tune == TUNE_SQ) {
1589         SetTargetBudgetSq(
1590             sequence_control_set_ptr,
1591             picture_control_set_ptr,
1592             context_ptr);
1593     }
1594     else if (sequence_control_set_ptr->static_config.tune == TUNE_VMAF) {
1595         set_target_budget_vmaf(
1596             sequence_control_set_ptr,
1597             picture_control_set_ptr,
1598             context_ptr);
1599     }
1600     else {
1601         eb_vp9_set_target_budget_oq(
1602             sequence_control_set_ptr,
1603             picture_control_set_ptr,
1604             context_ptr);
1605     }
1606 
1607     // Set the percentage based thresholds
1608     eb_vp9_derive_default_segments(
1609         picture_control_set_ptr,
1610         context_ptr);
1611 
1612     // Compute the cost of the refinements
1613     compute_refinement_cost(
1614         sequence_control_set_ptr,
1615         picture_control_set_ptr,
1616         context_ptr);
1617 
1618     // Derive LCU score
1619     eb_vp9_derive_sb_score(
1620         sequence_control_set_ptr,
1621         picture_control_set_ptr,
1622         context_ptr);
1623 
1624     // Remove the outliers
1625     perform_outlier_removal(
1626         sequence_control_set_ptr,
1627         picture_control_set_ptr->parent_pcs_ptr,
1628         context_ptr);
1629 
1630     // Perform Budgetting
1631     eb_vp9_derive_optimal_budget_per_sb(
1632         sequence_control_set_ptr,
1633         picture_control_set_ptr,
1634         context_ptr);
1635 
1636     // Set the search method using the LCU cost (mapping)
1637     eb_vp9_derive_search_method(
1638         sequence_control_set_ptr,
1639         picture_control_set_ptr,
1640         context_ptr);
1641 
1642 }
1643 
1644 /******************************************************
1645 * Derive Mode Decision Config Settings for SQ
1646 Input   : encoder mode and tune
1647 Output  : EncDec Kernel signal(s)
1648 ******************************************************/
eb_vp9_signal_derivation_mode_decision_config_kernel_sq(PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1649 EbErrorType eb_vp9_signal_derivation_mode_decision_config_kernel_sq(
1650     PictureControlSet                *picture_control_set_ptr,
1651     ModeDecisionConfigurationContext *context_ptr)
1652 {
1653 
1654     EbErrorType return_error = EB_ErrorNone;
1655 
1656     context_ptr->adp_level = picture_control_set_ptr->parent_pcs_ptr->enc_mode;
1657 
1658     return return_error;
1659 }
1660 
1661 /******************************************************
1662 * Derive Mode Decision Config Settings for OQ
1663 Input   : encoder mode and tune
1664 Output  : EncDec Kernel signal(s)
1665 ******************************************************/
eb_vp9_signal_derivation_mode_decision_config_kernel_oq_vmaf(PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr)1666 EbErrorType eb_vp9_signal_derivation_mode_decision_config_kernel_oq_vmaf(
1667     PictureControlSet                *picture_control_set_ptr,
1668     ModeDecisionConfigurationContext *context_ptr)
1669 {
1670 
1671     EbErrorType return_error = EB_ErrorNone;
1672 
1673     context_ptr->adp_level = picture_control_set_ptr->parent_pcs_ptr->enc_mode;
1674 
1675     return return_error;
1676 }
1677 
1678 /********************************************
1679  * Constants
1680  ********************************************/
1681 #define ADD_CU_STOP_SPLIT             0   // Take into account & Stop Splitting
1682 #define ADD_CU_CONTINUE_SPLIT         1   // Take into account & Continue Splitting
1683 #define DO_NOT_ADD_CU_CONTINUE_SPLIT  2   // Do not take into account & Continue Splitting
1684 
1685 #define DEPTH_64 0   // Depth corresponding to the CU size
1686 #define DEPTH_32 1   // Depth corresponding to the CU size
1687 #define DEPTH_16 2   // Depth corresponding to the CU size
1688 #define DEPTH_8  3   // Depth corresponding to the CU size
1689 
1690 static const uint8_t incremental_count[PA_BLOCK_MAX_COUNT] = {
1691 
1692     //64x64
1693     0,
1694     //32x32
1695     4, 4,
1696     4, 4,
1697     //16x16
1698     0, 0, 0, 0,
1699     0, 4, 0, 4,
1700     0, 0, 0, 0,
1701     0, 4, 0, 4,
1702     //8x8
1703     0, 0, 0, 0, 0, 0, 0, 0,
1704     0, 0, 0, 0, 0, 0, 0, 0,
1705     0, 0, 0, 0, 0, 0, 0, 0,
1706     0, 0, 0, 4, 0, 0, 0, 4,
1707     0, 0, 0, 0, 0, 0, 0, 0,
1708     0, 0, 0, 0, 0, 0, 0, 0,
1709     0, 0, 0, 0, 0, 0, 0, 0,
1710     0, 0, 0, 4, 0, 0, 0, 4
1711 
1712 };
1713 
1714 /*******************************************
1715 mdcSetDepth : set depth to be tested
1716 *******************************************/
1717 #define REFINEMENT_P        0x01
1718 #define REFINEMENT_Pp1      0x02
1719 #define REFINEMENT_Pp2      0x04
1720 #define REFINEMENT_Pp3      0x08
1721 #define REFINEMENT_Pm1      0x10
1722 #define REFINEMENT_Pm2      0x20
1723 #define REFINEMENT_Pm3      0x40
1724 
1725 #define PA_DEPTH_ONE_STEP     21
1726 #define PA_DEPTH_TWO_STEP      5
1727 #define PA_DEPTH_THREE_STEP    1
1728 
eb_vp9_MdcInterDepthDecision(ModeDecisionConfigurationContext * context_ptr,uint32_t origin_x,uint32_t origin_y,uint32_t end_depth,uint32_t block_index)1729 void eb_vp9_MdcInterDepthDecision(
1730     ModeDecisionConfigurationContext *context_ptr,
1731     uint32_t                          origin_x,
1732     uint32_t                          origin_y,
1733     uint32_t                          end_depth,
1734     uint32_t                          block_index)
1735 {
1736 
1737     uint32_t leftblock_index;
1738     uint32_t topblock_index;
1739     uint32_t topLeftblock_index;
1740     uint32_t depth_zero_candidate_block_index;
1741     uint32_t depth_one_candidate_block_index = block_index;
1742     uint32_t depth_two_candidate_block_index = block_index;
1743     uint64_t depth_n_cost = 0;
1744     uint64_t depth_n_plus_one_cost = 0;
1745     uint64_t depth_n_part_cost = 0;
1746     uint64_t depth_n_plus_one_part_cost = 0;
1747 
1748     MdcpLocalCodingUnit *local_cu_array = context_ptr->local_cu_array;
1749 
1750     /*** Stage 0: Inter depth decision: depth 2 vs depth 3 ***/
1751     // Walks to the last coded 8x8 block for merging
1752     uint8_t  group_of8x8_blocks_count = context_ptr->group_of8x8_blocks_count;
1753     uint8_t  group_of16x16_blocks_count = context_ptr->group_of16x16_blocks_count;
1754     if ((GROUP_OF_4_8x8_BLOCKS(origin_x, origin_y))) {
1755 
1756         group_of8x8_blocks_count++;
1757 
1758         // From the last coded cu index, get the indices of the left, top, and top left cus
1759         leftblock_index = block_index - PA_DEPTH_THREE_STEP;
1760         topblock_index = leftblock_index - PA_DEPTH_THREE_STEP;
1761         topLeftblock_index = topblock_index - PA_DEPTH_THREE_STEP;
1762 
1763         // From the top left index, get the index of the candidate pu for merging
1764         depth_two_candidate_block_index = topLeftblock_index - 1;
1765 
1766         // Compute depth N cost
1767 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1768         get_partition_cost(
1769             picture_control_set_ptr,
1770             BLOCK_16X16,
1771             PARTITION_NONE,
1772             (local_cu_array[depth_two_candidate_block_index]).partition_context,
1773             &depth_n_part_cost);
1774 #endif
1775         depth_n_cost =
1776             (local_cu_array[depth_two_candidate_block_index]).early_cost + depth_n_part_cost;
1777 
1778         if (end_depth < 3) {
1779 
1780             (local_cu_array[depth_two_candidate_block_index]).early_split_flag = EB_FALSE;
1781             (local_cu_array[depth_two_candidate_block_index]).early_cost = depth_n_cost;
1782         }
1783         else {
1784             // Compute depth N+1 cost
1785 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1786             get_partition_cost(
1787                 picture_control_set_ptr,
1788                 BLOCK_16X16,
1789                 PARTITION_SPLIT,
1790                 (local_cu_array[depth_two_candidate_block_index]).partition_context,
1791                 &depth_n_plus_one_part_cost);
1792 #endif
1793             depth_n_plus_one_cost =
1794                 (local_cu_array[block_index]).early_cost +
1795                 (local_cu_array[leftblock_index]).early_cost +
1796                 (local_cu_array[topblock_index]).early_cost +
1797                 (local_cu_array[topLeftblock_index]).early_cost + depth_n_plus_one_part_cost;
1798 
1799             if (depth_n_cost <= depth_n_plus_one_cost) {
1800 
1801                 // If the cost is low enough to warrant not spliting further:
1802                 // 1. set the split flag of the candidate pu for merging to false
1803                 // 2. update the last pu index
1804                 (local_cu_array[depth_two_candidate_block_index]).early_split_flag = EB_FALSE;
1805                 (local_cu_array[depth_two_candidate_block_index]).early_cost = depth_n_cost;
1806 
1807             }
1808             else {
1809                 // If the cost is not low enough:
1810                 // update the cost of the candidate pu for merging
1811                 // this update is required for the next inter depth decision
1812                 (&local_cu_array[depth_two_candidate_block_index])->early_cost = depth_n_plus_one_cost;
1813             }
1814 
1815         }
1816     }
1817 
1818     // Walks to the last coded 16x16 block for merging
1819     if (GROUP_OF_4_16x16_BLOCKS(pa_get_block_stats(depth_two_candidate_block_index)->origin_x, pa_get_block_stats(depth_two_candidate_block_index)->origin_y) &&
1820         (group_of8x8_blocks_count == 4)) {
1821 
1822         group_of8x8_blocks_count = 0;
1823         group_of16x16_blocks_count++;
1824 
1825         // From the last coded pu index, get the indices of the left, top, and top left pus
1826         leftblock_index = depth_two_candidate_block_index - PA_DEPTH_TWO_STEP;
1827         topblock_index = leftblock_index - PA_DEPTH_TWO_STEP;
1828         topLeftblock_index = topblock_index - PA_DEPTH_TWO_STEP;
1829 
1830         // From the top left index, get the index of the candidate pu for merging
1831         depth_one_candidate_block_index = topLeftblock_index - 1;
1832 
1833         if (pa_get_block_stats(depth_one_candidate_block_index)->depth == 1) {
1834 
1835             // Compute depth N cost
1836 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1837             get_partition_cost(
1838                 picture_control_set_ptr,
1839                 BLOCK_32X32,
1840                 PARTITION_NONE,
1841                 local_cu_array[depth_one_candidate_block_index].partition_context,
1842                 &depth_n_part_cost);
1843 #endif
1844 
1845             depth_n_cost =
1846                 local_cu_array[depth_one_candidate_block_index].early_cost + depth_n_part_cost;
1847             if (end_depth < 2) {
1848 
1849                 local_cu_array[depth_one_candidate_block_index].early_split_flag = EB_FALSE;
1850                 local_cu_array[depth_one_candidate_block_index].early_cost = depth_n_cost;
1851 
1852             }
1853             else {
1854 
1855                 // Compute depth N+1 cost
1856 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1857                 get_partition_cost(
1858                     picture_control_set_ptr,
1859                     BLOCK_32X32,
1860                     PARTITION_SPLIT,
1861                     local_cu_array[depth_one_candidate_block_index].partition_context,
1862                     &depth_n_plus_one_part_cost);
1863 #endif
1864                 depth_n_plus_one_cost = local_cu_array[depth_two_candidate_block_index].early_cost +
1865                     local_cu_array[leftblock_index].early_cost +
1866                     local_cu_array[topblock_index].early_cost +
1867                     local_cu_array[topLeftblock_index].early_cost +
1868                     depth_n_plus_one_part_cost;
1869 
1870                 // Inter depth comparison: depth 1 vs depth 2
1871                 if (depth_n_cost <= depth_n_plus_one_cost) {
1872                     // If the cost is low enough to warrant not spliting further:
1873                     // 1. set the split flag of the candidate pu for merging to false
1874                     // 2. update the last pu index
1875                     local_cu_array[depth_one_candidate_block_index].early_split_flag = EB_FALSE;
1876                     local_cu_array[depth_one_candidate_block_index].early_cost = depth_n_cost;
1877                 }
1878                 else {
1879                     // If the cost is not low enough:
1880                     // update the cost of the candidate pu for merging
1881                     // this update is required for the next inter depth decision
1882                     local_cu_array[depth_one_candidate_block_index].early_cost = depth_n_plus_one_cost;
1883                 }
1884             }
1885         }
1886     }
1887 
1888     // Stage 2: Inter depth decision: depth 0 vs depth 1
1889 
1890     // Walks to the last coded 32x32 block for merging
1891     // Stage 2 isn't performed in I slices since the abcense of 64x64 candidates
1892     if (GROUP_OF_4_32x32_BLOCKS(pa_get_block_stats(depth_one_candidate_block_index)->origin_x, pa_get_block_stats(depth_one_candidate_block_index)->origin_y) &&
1893         (group_of16x16_blocks_count == 4)) {
1894 
1895         group_of16x16_blocks_count = 0;
1896 
1897         // From the last coded pu index, get the indices of the left, top, and top left pus
1898         leftblock_index = depth_one_candidate_block_index - PA_DEPTH_ONE_STEP;
1899         topblock_index = leftblock_index - PA_DEPTH_ONE_STEP;
1900         topLeftblock_index = topblock_index - PA_DEPTH_ONE_STEP;
1901 
1902         // From the top left index, get the index of the candidate pu for merging
1903         depth_zero_candidate_block_index = topLeftblock_index - 1;
1904 
1905         if (pa_get_block_stats(depth_zero_candidate_block_index)->depth == 0) {
1906 
1907             // Compute depth N cost
1908 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1909             get_partition_cost(
1910                 picture_control_set_ptr,
1911                 BLOCK_64X64,
1912                 PARTITION_NONE,
1913                 (&local_cu_array[depth_zero_candidate_block_index])->partition_context,
1914                 &depth_n_part_cost);
1915 #endif
1916             depth_n_cost = (&local_cu_array[depth_zero_candidate_block_index])->early_cost + depth_n_part_cost;
1917 
1918             if (end_depth < 1) {
1919                 (&local_cu_array[depth_zero_candidate_block_index])->early_split_flag = EB_FALSE;
1920             } else {
1921                 // Compute depth N+1 cost
1922 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1923                 get_partition_cost(
1924                     picture_control_set_ptr,
1925                     BLOCK_64X64,
1926                     PARTITION_SPLIT,
1927                     (&local_cu_array[depth_zero_candidate_block_index])->partition_context,
1928                     &depth_n_plus_one_part_cost);
1929 #endif
1930                 depth_n_plus_one_cost =
1931                     local_cu_array[depth_one_candidate_block_index].early_cost +
1932                     local_cu_array[leftblock_index].early_cost +
1933                     local_cu_array[topblock_index].early_cost +
1934                     local_cu_array[topLeftblock_index].early_cost +
1935                     depth_n_plus_one_part_cost;
1936 
1937                 // Inter depth comparison: depth 0 vs depth 1
1938                 if (depth_n_cost <= depth_n_plus_one_cost) {
1939                     // If the cost is low enough to warrant not spliting further:
1940                     // 1. set the split flag of the candidate pu for merging to false
1941                     // 2. update the last pu index
1942                     (&local_cu_array[depth_zero_candidate_block_index])->early_split_flag = EB_FALSE;
1943                 }
1944             }
1945         }
1946     }
1947 
1948     context_ptr->group_of8x8_blocks_count = group_of8x8_blocks_count;
1949     context_ptr->group_of16x16_blocks_count = group_of16x16_blocks_count;
1950 }
1951 
prediction_partition_loop(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr,SbUnit * sb_ptr,uint32_t start_depth,uint32_t end_depth)1952 static void prediction_partition_loop(
1953     SequenceControlSet               *sequence_control_set_ptr,
1954     PictureControlSet                *picture_control_set_ptr,
1955     ModeDecisionConfigurationContext *context_ptr,
1956     SbUnit                           *sb_ptr,
1957     uint32_t                          start_depth,
1958     uint32_t                          end_depth)
1959 {
1960     MACROBLOCKD *const xd = context_ptr->e_mbd;
1961 
1962     MdcpLocalCodingUnit *local_cu_array = context_ptr->local_cu_array;
1963     MdcpLocalCodingUnit *block_ptr;
1964 
1965     SbParams *sb_params = &sequence_control_set_ptr->sb_params_array[sb_ptr->sb_index];
1966 
1967     uint32_t block_index_in_rater_scan;
1968     uint32_t block_index;
1969 
1970     VP9_COMP   *cpi = picture_control_set_ptr->parent_pcs_ptr->cpi;
1971 
1972 #if SEG_SUPPORT
1973     VP9_COMMON *const cm = &cpi->common;
1974     struct segmentation *const seg = &cm->seg;
1975     const int qindex = eb_vp9_get_qindex(seg, sb_ptr->segment_id, picture_control_set_ptr->base_qindex);
1976 #else
1977     const int qindex = picture_control_set_ptr->base_qindex;
1978 #endif
1979     int RDMULT = eb_vp9_compute_rd_mult(cpi, qindex);
1980     context_ptr->rd_mult_sad = (int)MAX(round(sqrtf((float)RDMULT / 128) * 128), 1);
1981 
1982     for (block_index = 0; block_index < PA_BLOCK_MAX_COUNT; ++block_index) {
1983 
1984         block_ptr = &local_cu_array[block_index];
1985 
1986 #if 0 // Hsan: partition rate not helping @ open loop partitioning
1987         // Hsan: neighbor not generated @ open loop partitioning
1988         block_ptr->partition_context = 0;
1989 #endif
1990         local_cu_array[block_index].slected_cu = EB_FALSE;
1991         local_cu_array[block_index].stop_split = EB_FALSE;
1992 
1993         block_index_in_rater_scan = MD_SCAN_TO_RASTER_SCAN[block_index];
1994 
1995         if (sb_params->pa_raster_scan_block_validity[block_index_in_rater_scan])
1996         {
1997             uint32_t depth;
1998             PaBlockStats *block_stats_ptr = pa_get_block_stats(block_index);
1999 
2000             depth = block_stats_ptr->depth;
2001             block_ptr->early_split_flag = (depth < end_depth) ? EB_TRUE : EB_FALSE;
2002 
2003             if (depth >= start_depth && depth <= end_depth) {
2004 
2005                 if (picture_control_set_ptr->slice_type != I_SLICE) {
2006 
2007                     MeCuResults * me_pu_result = &picture_control_set_ptr->parent_pcs_ptr->me_results[sb_ptr->sb_index][block_index_in_rater_scan];
2008 
2009                     if (me_pu_result->distortion_direction[0].direction == BI_PRED) {
2010                         context_ptr->candidate_ptr->mode_info->ref_frame[0] = LAST_FRAME;
2011                         context_ptr->candidate_ptr->mode_info->ref_frame[1] = ALTREF_FRAME;
2012                     }
2013                     else if (me_pu_result->distortion_direction[0].direction == UNI_PRED_LIST_0) {
2014                         context_ptr->candidate_ptr->mode_info->ref_frame[0] = LAST_FRAME;
2015                         context_ptr->candidate_ptr->mode_info->ref_frame[1] = INTRA_FRAME;
2016                     }
2017                     else { // if (me_pu_result->distortion_direction[0].direction == UNI_PRED_LIST_1)
2018                         context_ptr->candidate_ptr->mode_info->ref_frame[0] = ALTREF_FRAME;
2019                         context_ptr->candidate_ptr->mode_info->ref_frame[1] = INTRA_FRAME;
2020                     }
2021 
2022                     // Hsan: what's the best mode for rate simulation
2023                     context_ptr->candidate_ptr->mode_info->mode = NEARMV;
2024 
2025                     if (context_ptr->candidate_ptr->mode_info->ref_frame[0] == ALTREF_FRAME) {
2026                         context_ptr->candidate_ptr->mode_info->mv[0].as_mv.col = me_pu_result->x_mv_l1 << 1;
2027                         context_ptr->candidate_ptr->mode_info->mv[0].as_mv.row = me_pu_result->y_mv_l1 << 1;
2028                         context_ptr->candidate_ptr->mode_info->mv[1].as_mv.col = me_pu_result->x_mv_l1 << 1;
2029                         context_ptr->candidate_ptr->mode_info->mv[1].as_mv.row = me_pu_result->y_mv_l1 << 1;
2030                     }
2031                     else {
2032                         context_ptr->candidate_ptr->mode_info->mv[0].as_mv.col = me_pu_result->x_mv_l0 << 1;
2033                         context_ptr->candidate_ptr->mode_info->mv[0].as_mv.row = me_pu_result->y_mv_l0 << 1;
2034                         context_ptr->candidate_ptr->mode_info->mv[1].as_mv.col = me_pu_result->x_mv_l1 << 1;
2035                         context_ptr->candidate_ptr->mode_info->mv[1].as_mv.row = me_pu_result->y_mv_l1 << 1;
2036                     }
2037 
2038                     // Set above_mi and left_mi
2039                     // Hsan: neighbor not generated @ open loop partitioning
2040                     context_ptr->e_mbd->above_mi = NULL;
2041                     context_ptr->e_mbd->left_mi  = NULL;
2042 
2043                     estimate_ref_frame_costs(
2044                         &picture_control_set_ptr->parent_pcs_ptr->cpi->common,
2045                         xd,
2046                         0,// segment_id
2047                         context_ptr->ref_costs_single,
2048                         context_ptr->ref_costs_comp,
2049                         &context_ptr->comp_mode_p);
2050 
2051                     block_ptr->early_cost = inter_fast_cost(
2052                         picture_control_set_ptr,
2053                         0,
2054                         context_ptr->rd_mult_sad,
2055                         context_ptr->mbmi_ext,
2056                         context_ptr->e_mbd,
2057                         context_ptr->ref_costs_single,
2058                         context_ptr->ref_costs_comp,
2059                         context_ptr->comp_mode_p,
2060                         context_ptr->candidate_ptr,
2061                         me_pu_result->distortion_direction[0].distortion,
2062                         0);
2063 
2064                 }
2065                 else {
2066                     assert(0);
2067                 }
2068 
2069                 if (end_depth == 2) {
2070                     context_ptr->group_of8x8_blocks_count = depth == 2 ? incremental_count[block_index_in_rater_scan] : 0;
2071                 } else if (end_depth == 1) {
2072                     context_ptr->group_of16x16_blocks_count = depth == 1 ? incremental_count[block_index_in_rater_scan] : 0;
2073                 }
2074 
2075                 eb_vp9_MdcInterDepthDecision(
2076                     context_ptr,
2077                     block_stats_ptr->origin_x,
2078                     block_stats_ptr->origin_y,
2079                     end_depth,
2080                     block_index);
2081             }
2082             else {
2083                 block_ptr->early_cost = ~0u;
2084             }
2085 
2086         }
2087     }
2088 }
2089 
2090 static const uint8_t parentblock_index[PA_BLOCK_MAX_COUNT] =
2091 {
2092     0,
2093     0, 0, 0, 1, 2, 3, 5, 0, 1, 2, 3, 10, 0, 1, 2, 3, 15, 0, 1, 2, 3,
2094     21, 0, 0, 1, 2, 3, 5, 0, 1, 2, 3, 10, 0, 1, 2, 3, 15, 0, 1, 2, 3,
2095     42, 0, 0, 1, 2, 3, 5, 0, 1, 2, 3, 10, 0, 1, 2, 3, 15, 0, 1, 2, 3,
2096     36, 0, 0, 1, 2, 3, 5, 0, 1, 2, 3, 10, 0, 1, 2, 3, 15, 0, 1, 2, 3,
2097 };
2098 
mdc_refinement(MdcpLocalCodingUnit * local_cu_array,uint32_t block_index,uint32_t depth,uint8_t refinement_level,uint8_t lowest_level)2099 static EbErrorType mdc_refinement(
2100     MdcpLocalCodingUnit *local_cu_array,
2101     uint32_t              block_index,
2102     uint32_t              depth,
2103     uint8_t               refinement_level,
2104     uint8_t               lowest_level)
2105 {
2106     EbErrorType return_error = EB_ErrorNone;
2107 
2108     if (refinement_level & REFINEMENT_P) {
2109         if (lowest_level == REFINEMENT_P) {
2110             local_cu_array[block_index].stop_split = EB_TRUE;
2111         }
2112 
2113     }
2114     else {
2115         local_cu_array[block_index].slected_cu = EB_FALSE;
2116     }
2117 
2118     if (refinement_level & REFINEMENT_Pp1) {
2119 
2120         if (depth < 3 && block_index < 81) {
2121             local_cu_array[block_index + 1].slected_cu = EB_TRUE;
2122             local_cu_array[block_index + 1 + depth_offset[depth + 1]].slected_cu = EB_TRUE;
2123             local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1]].slected_cu = EB_TRUE;
2124             local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1]].slected_cu = EB_TRUE;
2125         }
2126         if (lowest_level == REFINEMENT_Pp1) {
2127             if (depth < 3 && block_index < 81) {
2128                 local_cu_array[block_index + 1].stop_split = EB_TRUE;
2129                 local_cu_array[block_index + 1 + depth_offset[depth + 1]].stop_split = EB_TRUE;
2130                 local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1]].stop_split = EB_TRUE;
2131                 local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1]].stop_split = EB_TRUE;
2132             }
2133         }
2134     }
2135 
2136     if (refinement_level & REFINEMENT_Pp2) {
2137         if (depth < 2 && block_index < 65) {
2138             local_cu_array[block_index + 1 + 1].slected_cu = EB_TRUE;
2139             local_cu_array[block_index + 1 + 1 + depth_offset[depth + 2]].slected_cu = EB_TRUE;
2140             local_cu_array[block_index + 1 + 1 + 2 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2141             local_cu_array[block_index + 1 + 1 + 3 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2142 
2143             local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1].slected_cu = EB_TRUE;
2144             local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].slected_cu = EB_TRUE;
2145             local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2146             local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2147 
2148             local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1].slected_cu = EB_TRUE;
2149             local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].slected_cu = EB_TRUE;
2150             local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2151             local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2152 
2153             local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1].slected_cu = EB_TRUE;
2154             local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].slected_cu = EB_TRUE;
2155             local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2156             local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].slected_cu = EB_TRUE;
2157         }
2158         if (lowest_level == REFINEMENT_Pp2) {
2159             if (depth < 2 && block_index < 65) {
2160                 local_cu_array[block_index + 1 + 1].stop_split = EB_TRUE;
2161                 local_cu_array[block_index + 1 + 1 + depth_offset[depth + 2]].stop_split = EB_TRUE;
2162                 local_cu_array[block_index + 1 + 1 + 2 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2163                 local_cu_array[block_index + 1 + 1 + 3 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2164 
2165                 local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1].stop_split = EB_TRUE;
2166                 local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].stop_split = EB_TRUE;
2167                 local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2168                 local_cu_array[block_index + 1 + depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2169 
2170                 local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1].stop_split = EB_TRUE;
2171                 local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].stop_split = EB_TRUE;
2172                 local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2173                 local_cu_array[block_index + 1 + 2 * depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2174 
2175                 local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1].stop_split = EB_TRUE;
2176                 local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + depth_offset[depth + 2]].stop_split = EB_TRUE;
2177                 local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + 2 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2178                 local_cu_array[block_index + 1 + 3 * depth_offset[depth + 1] + 1 + 3 * depth_offset[depth + 2]].stop_split = EB_TRUE;
2179             }
2180         }
2181     }
2182 
2183     if (refinement_level & REFINEMENT_Pp3) {
2184         uint8_t in_loop;
2185         uint8_t out_loop;
2186         uint8_t block_index = 2;
2187         if (depth == 0) {
2188 
2189             for (out_loop = 0; out_loop < 16; ++out_loop) {
2190                 for (in_loop = 0; in_loop < 4; ++in_loop) {
2191                     local_cu_array[++block_index].slected_cu = EB_TRUE;
2192 
2193                 }
2194                 block_index += block_index == 21 ? 2 : block_index == 42 ? 2 : block_index == 63 ? 2 : 1;
2195 
2196             }
2197             if (lowest_level == REFINEMENT_Pp3) {
2198                 block_index = 2;
2199                 for (out_loop = 0; out_loop < 16; ++out_loop) {
2200                     for (in_loop = 0; in_loop < 4; ++in_loop) {
2201                         local_cu_array[++block_index].stop_split = EB_TRUE;
2202                     }
2203                     block_index += block_index == 21 ? 2 : block_index == 42 ? 2 : block_index == 63 ? 2 : 1;
2204                 }
2205             }
2206         }
2207 
2208     }
2209 
2210     if (refinement_level & REFINEMENT_Pm1) {
2211         if (depth > 0) {
2212             local_cu_array[block_index - 1 - parentblock_index[block_index]].slected_cu = EB_TRUE;
2213         }
2214         if (lowest_level == REFINEMENT_Pm1) {
2215             if (depth > 0) {
2216                 local_cu_array[block_index - 1 - parentblock_index[block_index]].stop_split = EB_TRUE;
2217             }
2218         }
2219     }
2220 
2221     if (refinement_level & REFINEMENT_Pm2) {
2222         if (depth == 2) {
2223             local_cu_array[0].slected_cu = EB_TRUE;
2224         }
2225         if (depth == 3) {
2226             local_cu_array[1].slected_cu = EB_TRUE;
2227             local_cu_array[22].slected_cu = EB_TRUE;
2228             local_cu_array[43].slected_cu = EB_TRUE;
2229             local_cu_array[64].slected_cu = EB_TRUE;
2230         }
2231         if (lowest_level == REFINEMENT_Pm2) {
2232             if (depth == 2) {
2233                 local_cu_array[0].stop_split = EB_TRUE;
2234             }
2235             if (depth == 3) {
2236                 local_cu_array[1].stop_split = EB_TRUE;
2237                 local_cu_array[22].stop_split = EB_TRUE;
2238                 local_cu_array[43].stop_split = EB_TRUE;
2239                 local_cu_array[64].stop_split = EB_TRUE;
2240             }
2241         }
2242     }
2243 
2244     if (refinement_level & REFINEMENT_Pm3) {
2245         if (depth == 3) {
2246             local_cu_array[0].slected_cu = EB_TRUE;
2247         }
2248         if (lowest_level == REFINEMENT_Pm2) {
2249             if (depth == 3) {
2250                 local_cu_array[0].stop_split = EB_TRUE;
2251             }
2252         }
2253     }
2254 
2255     return return_error;
2256 }
2257 
refinement_prediction_loop(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,SbUnit * sb_ptr,uint32_t sb_index,ModeDecisionConfigurationContext * context_ptr)2258 static void refinement_prediction_loop(
2259     SequenceControlSet               *sequence_control_set_ptr,
2260     PictureControlSet                *picture_control_set_ptr,
2261     SbUnit                           *sb_ptr,
2262     uint32_t                          sb_index,
2263     ModeDecisionConfigurationContext *context_ptr)
2264 {
2265     MdcpLocalCodingUnit  *local_cu_array = context_ptr->local_cu_array;
2266     SbParams             *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2267     uint32_t              temporal_layer_index = picture_control_set_ptr->temporal_layer_index;
2268     uint32_t              block_index = 0;
2269     uint8_t               stationary_edge_over_time_flag = (&(picture_control_set_ptr->parent_pcs_ptr->sb_stat_array[sb_index]))->stationary_edge_over_time_flag;
2270 
2271     sb_ptr->pred64 = EB_FALSE;
2272 
2273     while (block_index < PA_BLOCK_MAX_COUNT)
2274     {
2275         if (sb_params->pa_raster_scan_block_validity[MD_SCAN_TO_RASTER_SCAN[block_index]] && (local_cu_array[block_index].early_split_flag == EB_FALSE))
2276         {
2277             local_cu_array[block_index].slected_cu = EB_TRUE;
2278             sb_ptr->pred64 = (block_index == 0) ? EB_TRUE : sb_ptr->pred64;
2279 
2280             PaBlockStats   *block_stats_ptr = pa_get_block_stats(block_index);
2281             uint32_t depth = block_stats_ptr->depth;
2282 
2283             uint8_t refinement_level;
2284 
2285             if (sb_ptr->picture_control_set_ptr->slice_type == I_SLICE) {
2286 
2287                 {
2288                     uint8_t lowest_level = 0x00;
2289 
2290                     if (sequence_control_set_ptr->input_resolution == INPUT_SIZE_4K_RANGE)
2291                         refinement_level = ndp_refinement_control_islice[depth];
2292                     else
2293                         refinement_level = ndp_refinement_control_islice_sub4_k[depth];
2294 
2295                     if (depth <= 1 && stationary_edge_over_time_flag > 0) {
2296                         if (depth == 0)
2297                             refinement_level = Predp1 + Predp2 + Predp3;
2298                         else
2299                             refinement_level = Pred + Predp1 + Predp2;
2300 
2301                     }
2302                     lowest_level = (refinement_level & REFINEMENT_Pp3) ? REFINEMENT_Pp3 : (refinement_level & REFINEMENT_Pp2) ? REFINEMENT_Pp2 : (refinement_level & REFINEMENT_Pp1) ? REFINEMENT_Pp1 :
2303                         (refinement_level & REFINEMENT_P) ? REFINEMENT_P :
2304                         (refinement_level & REFINEMENT_Pm1) ? REFINEMENT_Pm1 : (refinement_level & REFINEMENT_Pm2) ? REFINEMENT_Pm2 : (refinement_level & REFINEMENT_Pm3) ? REFINEMENT_Pm3 : 0x00;
2305 
2306                     mdc_refinement(
2307                         &(*context_ptr->local_cu_array),
2308                         block_index,
2309                         depth,
2310                         refinement_level,
2311                         lowest_level);
2312                 }
2313             }
2314             else {
2315                 if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_SB_SWITCH_DEPTH_MODE && (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE)) {
2316                     refinement_level = Pred;
2317                 }
2318                 else
2319 
2320                     if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_OPEN_LOOP_DEPTH_MODE ||
2321                         (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_SB_SWITCH_DEPTH_MODE && picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_OPEN_LOOP_DEPTH_MODE))
2322 
2323                         refinement_level = ndp_refinement_control_nref[temporal_layer_index][depth];
2324                     else
2325                         refinement_level = ndp_refinement_control_fast[temporal_layer_index][depth];
2326 
2327                 if (picture_control_set_ptr->parent_pcs_ptr->cu8x8_mode == CU_8x8_MODE_1) {
2328                     refinement_level = ((refinement_level & REFINEMENT_Pp1) && depth == 2) ? refinement_level - REFINEMENT_Pp1 :
2329                         ((refinement_level & REFINEMENT_Pp2) && depth == 1) ? refinement_level - REFINEMENT_Pp2 :
2330                         ((refinement_level & REFINEMENT_Pp3) && depth == 0) ? refinement_level - REFINEMENT_Pp3 : refinement_level;
2331                 }
2332 
2333                 uint8_t lowest_level = 0x00;
2334 
2335                 lowest_level = (refinement_level & REFINEMENT_Pp3) ? REFINEMENT_Pp3 : (refinement_level & REFINEMENT_Pp2) ? REFINEMENT_Pp2 : (refinement_level & REFINEMENT_Pp1) ? REFINEMENT_Pp1 :
2336                     (refinement_level & REFINEMENT_P) ? REFINEMENT_P :
2337                     (refinement_level & REFINEMENT_Pm1) ? REFINEMENT_Pm1 : (refinement_level & REFINEMENT_Pm2) ? REFINEMENT_Pm2 : (refinement_level & REFINEMENT_Pm3) ? REFINEMENT_Pm3 : 0x00;
2338 
2339                 mdc_refinement(
2340                     &(*context_ptr->local_cu_array),
2341                     block_index,
2342                     depth,
2343                     refinement_level,
2344                     lowest_level);
2345             }
2346 
2347             block_index += depth_offset[depth];
2348 
2349         }
2350         else {
2351 
2352             block_index++;
2353         }
2354     } // End while 1 CU Loop
2355 }
2356 
forward_cu_to_mode_decision(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index,ModeDecisionConfigurationContext * context_ptr)2357 void forward_cu_to_mode_decision(
2358     SequenceControlSet               *sequence_control_set_ptr,
2359     PictureControlSet                *picture_control_set_ptr,
2360     uint32_t                          sb_index,
2361     ModeDecisionConfigurationContext *context_ptr)
2362 {
2363 
2364     uint8_t              block_index = 0;
2365     uint32_t             cu_class = DO_NOT_ADD_CU_CONTINUE_SPLIT;
2366     EB_BOOL              split_flag = EB_TRUE;
2367     MdcSbData           *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2368     SbParams            *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2369     MdcpLocalCodingUnit *local_cu_array = context_ptr->local_cu_array;
2370     EB_SLICE             slice_type = picture_control_set_ptr->slice_type;
2371 
2372     results_ptr->block_count = 0;
2373 
2374     block_index = 0;
2375 
2376     while (block_index < PA_BLOCK_MAX_COUNT)
2377     {
2378         PaBlockStats *block_stats_ptr = pa_get_block_stats(block_index);
2379 
2380         split_flag = EB_TRUE;
2381         if (sb_params->pa_raster_scan_block_validity[MD_SCAN_TO_RASTER_SCAN[block_index]])
2382         {
2383             switch (block_stats_ptr->depth) {
2384 
2385             case 0:
2386             case 1:
2387             case 2:
2388 
2389                 cu_class = DO_NOT_ADD_CU_CONTINUE_SPLIT;
2390 
2391                 if (slice_type == I_SLICE) {
2392                     cu_class = local_cu_array[block_index].slected_cu == EB_TRUE ? ADD_CU_CONTINUE_SPLIT : cu_class;
2393                     cu_class = local_cu_array[block_index].stop_split == EB_TRUE ? ADD_CU_STOP_SPLIT : cu_class;
2394                 }
2395                 else {
2396                     cu_class = local_cu_array[block_index].slected_cu == EB_TRUE ? ADD_CU_CONTINUE_SPLIT : cu_class;
2397                     cu_class = local_cu_array[block_index].stop_split == EB_TRUE ? ADD_CU_STOP_SPLIT : cu_class;
2398 
2399                 }
2400 
2401                 // Take into account MAX_CU_SIZE
2402                 cu_class = (block_stats_ptr->size > MAX_CU_SIZE || (slice_type == I_SLICE && block_stats_ptr->size > MAX_INTRA_SIZE)) ?
2403                     DO_NOT_ADD_CU_CONTINUE_SPLIT :
2404                     cu_class;
2405 
2406                 // Take into account MIN_CU_SIZE
2407                 cu_class = (block_stats_ptr->size == MIN_CU_SIZE || (slice_type == I_SLICE && block_stats_ptr->size == MIN_INTRA_SIZE)) ?
2408                     ADD_CU_STOP_SPLIT :
2409                     cu_class;
2410 
2411                 switch (cu_class) {
2412 
2413                 case ADD_CU_STOP_SPLIT:
2414                     // Stop
2415                     results_ptr->block_data_array[results_ptr->block_count].block_index = pa_to_ep_block_index[block_index];
2416                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2417 
2418                     break;
2419 
2420                 case ADD_CU_CONTINUE_SPLIT:
2421                     // Go Down + consider the current CU as candidate
2422                     results_ptr->block_data_array[results_ptr->block_count].block_index = pa_to_ep_block_index[block_index];
2423                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2424 
2425                     break;
2426 
2427                 case DO_NOT_ADD_CU_CONTINUE_SPLIT:
2428                     // Go Down + do not consider the current CU as candidate
2429                     split_flag = EB_TRUE;
2430                     break;
2431                 }
2432 
2433                 break;
2434             case 3:
2435                 results_ptr->block_data_array[results_ptr->block_count].block_index = pa_to_ep_block_index[block_index];
2436                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2437 
2438                 break;
2439 
2440             default:
2441                 results_ptr->block_data_array[results_ptr->block_count].block_index = pa_to_ep_block_index[block_index];
2442                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2443                 break;
2444             }
2445         }
2446 
2447         block_index += (split_flag == EB_TRUE) ? 1 : depth_offset[block_stats_ptr->depth];
2448 
2449     }
2450 }
2451 
early_mode_decision_sb(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,ModeDecisionConfigurationContext * context_ptr,SbUnit * sb_ptr)2452 static EbErrorType early_mode_decision_sb(
2453     SequenceControlSet               *sequence_control_set_ptr,
2454     PictureControlSet                *picture_control_set_ptr,
2455     ModeDecisionConfigurationContext *context_ptr,
2456     SbUnit                           *sb_ptr)
2457 {
2458 
2459     EbErrorType    return_error = EB_ErrorNone;
2460 
2461     EB_SLICE       slice_type = picture_control_set_ptr->slice_type;
2462 
2463     // Hsan: to evaluate after freezing a 1st M6
2464 #if SHUT_64x64_BASE_RESTRICTION
2465     uint32_t      start_depth = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0 && (sequence_control_set_ptr->static_config.tune == TUNE_SQ || sequence_control_set_ptr->static_config.tune == TUNE_VMAF)) ? DEPTH_32 : DEPTH_64;
2466 #else
2467     uint32_t      start_depth = (picture_control_set_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? DEPTH_32 : DEPTH_64;
2468 #endif
2469     uint32_t      end_depth = (slice_type == I_SLICE) ? DEPTH_8 : DEPTH_16;
2470 
2471     context_ptr->group_of8x8_blocks_count = 0;
2472     context_ptr->group_of16x16_blocks_count = 0;
2473 
2474     prediction_partition_loop(
2475         sequence_control_set_ptr,
2476         picture_control_set_ptr,
2477         context_ptr,
2478         sb_ptr,
2479         start_depth,
2480         end_depth);
2481 
2482     refinement_prediction_loop(
2483         sequence_control_set_ptr,
2484         picture_control_set_ptr,
2485         sb_ptr,
2486         sb_ptr->sb_index,
2487         context_ptr);
2488 
2489     forward_cu_to_mode_decision(
2490         sequence_control_set_ptr,
2491         picture_control_set_ptr,
2492         sb_ptr->sb_index,
2493         context_ptr);
2494 
2495     return return_error;
2496 
2497 }
2498 
2499 /******************************************************
2500 * Predict the LCU partitionning
2501 ******************************************************/
picture_depth_open_loop(ModeDecisionConfigurationContext * context_ptr,SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr)2502 void picture_depth_open_loop(
2503     ModeDecisionConfigurationContext *context_ptr,
2504     SequenceControlSet               *sequence_control_set_ptr,
2505     PictureControlSet                *picture_control_set_ptr) {
2506 
2507     for (int sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2508         early_mode_decision_sb(
2509             sequence_control_set_ptr,
2510             picture_control_set_ptr,
2511             context_ptr,
2512             picture_control_set_ptr->sb_ptr_array[sb_index]);
2513     }
2514 }
2515 
sb_depth_open_loop(ModeDecisionConfigurationContext * context_ptr,SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index)2516 void sb_depth_open_loop(
2517     ModeDecisionConfigurationContext *context_ptr,
2518     SequenceControlSet               *sequence_control_set_ptr,
2519     PictureControlSet                *picture_control_set_ptr,
2520     uint32_t                          sb_index) {
2521 
2522     early_mode_decision_sb(
2523         sequence_control_set_ptr,
2524         picture_control_set_ptr,
2525         context_ptr,
2526         picture_control_set_ptr->sb_ptr_array[sb_index]);
2527 }
2528 
sb_depth_85_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index)2529 void sb_depth_85_block(
2530     SequenceControlSet *sequence_control_set_ptr,
2531     PictureControlSet  *picture_control_set_ptr,
2532     uint32_t            sb_index)
2533 {
2534 
2535     EB_BOOL    split_flag;
2536     SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2537     MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2538 
2539     results_ptr->block_count = 0;
2540     uint16_t block_index = 0;
2541 
2542     while (block_index < EP_BLOCK_MAX_COUNT)
2543     {
2544         split_flag = EB_TRUE;
2545 
2546         const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2547         uint8_t depth = ep_block_stats_ptr->depth;
2548         if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2549         {
2550             switch (depth) {
2551 #if INTRA_4x4_SB_DEPTH_84_85
2552             case 0:
2553                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2554                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2555                 break;
2556             case 1:
2557                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2558                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2559                 break;
2560             case 2:
2561                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2562                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2563                 break;
2564             case 3:
2565                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2566                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2567                 break;
2568             case 4:
2569                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2570                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2571                 break;
2572 #else
2573             case 0:
2574                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2575                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2576                 break;
2577 
2578             case 1:
2579                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2580                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2581                 break;
2582 
2583             case 2:
2584                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2585                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2586                 break;
2587 
2588             case 3:
2589                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2590                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2591                 break;
2592 #endif
2593             }
2594         }
2595 
2596         block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2597     }
2598 }
2599 
sb_depth_84_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index)2600 void sb_depth_84_block(
2601     SequenceControlSet *sequence_control_set_ptr,
2602     PictureControlSet  *picture_control_set_ptr,
2603     uint32_t            sb_index)
2604 {
2605 
2606     EB_BOOL split_flag;
2607     SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2608     MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2609 
2610     results_ptr->block_count = 0;
2611     uint16_t block_index = 0;
2612 
2613     while (block_index < EP_BLOCK_MAX_COUNT)
2614     {
2615         split_flag = EB_TRUE;
2616 
2617         const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2618         uint8_t depth = ep_block_stats_ptr->depth;
2619         if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2620         {
2621             switch (depth) {
2622 
2623 #if INTRA_4x4_SB_DEPTH_84_85
2624             case 0:
2625                 split_flag = EB_TRUE;
2626                 break;
2627             case 1:
2628                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2629                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2630                 break;
2631             case 2:
2632                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2633                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2634                 break;
2635             case 3:
2636                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2637                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2638                 break;
2639             case 4:
2640                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2641                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2642                 break;
2643 #else
2644             case 0:
2645                 split_flag = EB_TRUE;
2646                 break;
2647 
2648             case 1:
2649                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2650                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2651                 break;
2652 
2653             case 2:
2654                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2655                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2656                 break;
2657 
2658             case 3:
2659                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2660                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2661                 break;
2662 #endif
2663             }
2664         }
2665 
2666         block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2667     }
2668 }
2669 
picture_depth_85_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr)2670 void picture_depth_85_block(
2671     SequenceControlSet *sequence_control_set_ptr,
2672     PictureControlSet  *picture_control_set_ptr)
2673 {
2674 
2675     uint32_t sb_index;
2676     EB_BOOL split_flag;
2677 
2678     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2679 
2680         SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2681         MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2682 
2683         results_ptr->block_count = 0;
2684         uint16_t block_index = 0;
2685 
2686         while (block_index < EP_BLOCK_MAX_COUNT)
2687         {
2688             split_flag = EB_TRUE;
2689 
2690             const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2691             uint8_t depth = ep_block_stats_ptr->depth;
2692             if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2693             {
2694                 switch (depth) {
2695 #if INTRA_4x4_SB_DEPTH_84_85
2696                 case 0:
2697                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2698                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2699                     break;
2700                 case 1:
2701                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2702                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2703                     break;
2704                 case 2:
2705                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2706                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2707                     break;
2708                 case 3:
2709                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2710                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2711                     break;
2712                 case 4:
2713                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2714                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2715                     break;
2716 #else
2717                 case 0:
2718                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2719                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2720                     break;
2721 
2722                 case 1:
2723                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2724                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2725                     break;
2726 
2727                 case 2:
2728                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2729                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2730                     break;
2731 
2732                 case 3:
2733                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2734                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2735                     break;
2736 #endif
2737                 }
2738             }
2739 
2740             block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2741         }
2742     }
2743 
2744 }
2745 
picture_depth_84_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr)2746 void picture_depth_84_block(
2747     SequenceControlSet *sequence_control_set_ptr,
2748     PictureControlSet  *picture_control_set_ptr)
2749 {
2750 
2751     uint32_t sb_index;
2752     EB_BOOL split_flag;
2753 
2754     for (sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2755 
2756         SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2757         MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2758 
2759         results_ptr->block_count = 0;
2760         uint16_t block_index = 0;
2761 
2762         while (block_index < EP_BLOCK_MAX_COUNT)
2763         {
2764             split_flag = EB_TRUE;
2765 
2766             const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2767             uint8_t depth = ep_block_stats_ptr->depth;
2768             if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2769             {
2770                 switch (depth) {
2771 #if INTRA_4x4_I_SLICE
2772                 case 0:
2773                     split_flag = EB_TRUE;
2774                     break;
2775                 case 1:
2776                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2777                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2778                     break;
2779                 case 2:
2780                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2781                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2782                     break;
2783                 case 3:
2784                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2785                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2786                     break;
2787                 case 4:
2788                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2789                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2790                     break;
2791 #else
2792                 case 0:
2793                     split_flag = EB_TRUE;
2794                     break;
2795                 case 1:
2796                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2797                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2798                     break;
2799                 case 2:
2800                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2801                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2802                     break;
2803                 case 3:
2804                     results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2805                     results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2806                     break;
2807 #endif
2808                 }
2809             }
2810             block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2811         }
2812     }
2813 }
2814 
sb_depth_8x8_16x16_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index)2815 void sb_depth_8x8_16x16_block(
2816     SequenceControlSet *sequence_control_set_ptr,
2817     PictureControlSet  *picture_control_set_ptr,
2818     uint32_t            sb_index)
2819 {
2820 
2821     EB_BOOL split_flag;
2822     SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2823     MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2824 
2825     results_ptr->block_count = 0;
2826     uint16_t block_index = 0;
2827 
2828     while (block_index < EP_BLOCK_MAX_COUNT)
2829     {
2830         split_flag = EB_TRUE;
2831 
2832         const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2833         uint8_t depth = ep_block_stats_ptr->depth;
2834         if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2835         {
2836             switch (depth) {
2837             case 0:
2838                 split_flag = EB_TRUE;
2839                 break;
2840             case 1:
2841                 split_flag = EB_TRUE;
2842                 break;
2843             case 2:
2844                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2845                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_TRUE;
2846                 break;
2847             case 3:
2848                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2849                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2850                 break;
2851             }
2852         }
2853 
2854         block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2855     }
2856 }
2857 
sb_depth_16x16_block(SequenceControlSet * sequence_control_set_ptr,PictureControlSet * picture_control_set_ptr,uint32_t sb_index)2858 void sb_depth_16x16_block(
2859     SequenceControlSet *sequence_control_set_ptr,
2860     PictureControlSet  *picture_control_set_ptr,
2861     uint32_t            sb_index)
2862 {
2863 
2864     EB_BOOL split_flag;
2865 
2866     SbParams  *sb_params = &sequence_control_set_ptr->sb_params_array[sb_index];
2867     MdcSbData *results_ptr = &picture_control_set_ptr->mdc_sb_data_array[sb_index];
2868 
2869     results_ptr->block_count = 0;
2870     uint16_t block_index = 0;
2871 
2872     while (block_index < EP_BLOCK_MAX_COUNT)
2873     {
2874         split_flag = EB_TRUE;
2875 
2876         const EpBlockStats *ep_block_stats_ptr = ep_get_block_stats(block_index);
2877         uint8_t depth = ep_block_stats_ptr->depth;
2878         if (sb_params->ep_scan_block_validity[block_index] && ep_block_stats_ptr->shape == PART_N)
2879         {
2880             switch (depth) {
2881             case 0:
2882                 split_flag = EB_TRUE;
2883                 break;
2884             case 1:
2885                 split_flag = EB_TRUE;
2886                 break;
2887             case 2:
2888                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2889                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2890                 break;
2891             case 3:
2892                 results_ptr->block_data_array[results_ptr->block_count].block_index = block_index;
2893                 results_ptr->block_data_array[results_ptr->block_count++].split_flag = split_flag = EB_FALSE;
2894                 break;
2895             }
2896         }
2897         block_index += (split_flag == EB_FALSE) ? sq_depth_offset[depth] : nsq_depth_offset[depth];
2898     }
2899 }
2900 
2901 #if BEA
2902 #define MAX_DELTA_QINDEX 80
2903 #define DELTA_QINDEX_SEGMENTS 8
qpm_derive_bea(ModeDecisionConfigurationContext * context_ptr,PictureControlSet * picture_control_set_ptr,SequenceControlSet * sequence_control_set_ptr)2904 EbErrorType qpm_derive_bea(
2905     ModeDecisionConfigurationContext      *context_ptr,
2906     PictureControlSet                       *picture_control_set_ptr,
2907     SequenceControlSet                    *sequence_control_set_ptr)
2908 {
2909 
2910     EbErrorType     return_error = EB_ErrorNone;
2911     SbUnit       *sb_ptr;
2912     int64_t           non_moving_index_distance;
2913     int32_t           non_moving_weight = MAX_DELTA_QINDEX;
2914     int                             non_moving_delta_qp;
2915     int non_moving_delta_qp_temp;
2916     if (picture_control_set_ptr->slice_type == 2)
2917         non_moving_weight = MAX_DELTA_QINDEX;
2918     else if (picture_control_set_ptr->temporal_layer_index == 0)
2919         non_moving_weight = MAX_DELTA_QINDEX / 2;
2920     else if (picture_control_set_ptr->temporal_layer_index == 1)
2921         non_moving_weight = MAX_DELTA_QINDEX / 4;
2922 #if BEA_SCENE_CHANGE
2923     if (picture_control_set_ptr->parent_pcs_ptr->scene_change_flag)
2924         non_moving_weight = non_moving_weight / 2;
2925 #endif
2926 
2927     //else
2928     //    non_moving_weight = 1;
2929 
2930     uint32_t distanceRatio = (uint32_t)~0;
2931 
2932     for (int sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
2933         sb_ptr = picture_control_set_ptr->sb_ptr_array[sb_index];
2934         if (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance != 0 && picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance != 0) {
2935             distanceRatio = (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance > picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance) ? abs(picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance * 100 / picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance) : abs(picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance * 100 / picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance);
2936         }
2937 
2938         //if (distanceRatio >= BEA_DISTANSE_RATIO_T0) {
2939         //    non_moving_weight = 1;
2940         //}
2941 
2942         non_moving_index_distance = (int32_t)picture_control_set_ptr->parent_pcs_ptr->non_moving_index_array[sb_index] - (int32_t)picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score;
2943 
2944         if (non_moving_index_distance < 0) {
2945 
2946             non_moving_delta_qp = (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance != 0) ? (int8_t)((non_moving_weight * non_moving_index_distance) / picture_control_set_ptr->parent_pcs_ptr->non_moving_index_min_distance) : 0;
2947             non_moving_delta_qp_temp = (non_moving_delta_qp * (DELTA_QINDEX_SEGMENTS - 2) / non_moving_weight);
2948             non_moving_delta_qp = (non_moving_delta_qp * (DELTA_QINDEX_SEGMENTS - 2) / non_moving_weight)* non_moving_weight / (DELTA_QINDEX_SEGMENTS - 2);
2949             sb_ptr->segment_id = CLIP3(1, DELTA_QINDEX_SEGMENTS - 1, 1 - non_moving_delta_qp_temp);
2950             //sb_ptr->segment_id = CLIP3( 1, DELTA_QINDEX_SEGMENTS - 1, 1 +  (-non_moving_delta_qp * (DELTA_QINDEX_SEGMENTS - 2) / non_moving_weight));
2951         }
2952         else {
2953             non_moving_delta_qp = (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance != 0) ? (int8_t)((non_moving_index_distance * 100) / (picture_control_set_ptr->parent_pcs_ptr->non_moving_index_max_distance)) : 0;
2954             non_moving_delta_qp = (non_moving_delta_qp + 50) / 100;
2955             sb_ptr->segment_id = CLIP3(0, 1, 1 + -non_moving_delta_qp);
2956 
2957             if (non_moving_delta_qp) {
2958                 if (picture_control_set_ptr->slice_type == 2)
2959                     non_moving_delta_qp = 8;
2960                 else if (picture_control_set_ptr->temporal_layer_index == 0)
2961                     non_moving_delta_qp = 4;
2962                 else if (picture_control_set_ptr->temporal_layer_index == 1)
2963                     non_moving_delta_qp = 2;
2964 
2965             }
2966 
2967         }
2968         context_ptr->qindex_delta[sb_ptr->segment_id] = non_moving_delta_qp;
2969         picture_control_set_ptr->segment_counts[sb_ptr->segment_id]++;
2970     }
2971     return return_error;
2972 }
2973 #endif
2974 
2975 /******************************************************
2976  * Mode Decision Configuration Kernel
2977  ******************************************************/
eb_vp9_mode_decision_configuration_kernel(void * input_ptr)2978 void* eb_vp9_mode_decision_configuration_kernel(void *input_ptr)
2979 {
2980     // Context & SCS & PCS
2981     ModeDecisionConfigurationContext *context_ptr = (ModeDecisionConfigurationContext*) input_ptr;
2982     PictureControlSet                *picture_control_set_ptr;
2983     SequenceControlSet               *sequence_control_set_ptr;
2984 
2985     // Input
2986     EbObjectWrapper                  *rate_control_results_wrapper_ptr;
2987     RateControlResults               *rate_control_results_ptr;
2988 
2989     // Output
2990     EbObjectWrapper                  *enc_dec_tasks_wrapper_ptr;
2991     EncDecTasks                      *enc_dec_tasks_ptr;
2992     uint32_t                          picture_width_in_sb;
2993 
2994     for(;;) {
2995 
2996         // Get RateControl Results
2997         eb_vp9_get_full_object(
2998             context_ptr->rate_control_input_fifo_ptr,
2999             &rate_control_results_wrapper_ptr);
3000 
3001         rate_control_results_ptr = (RateControlResults*)rate_control_results_wrapper_ptr->object_ptr;
3002         picture_control_set_ptr = (PictureControlSet  *)rate_control_results_ptr->picture_control_set_wrapper_ptr->object_ptr;
3003         sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
3004 
3005         // Mode Decision Configuration Kernel Signal(s) derivation
3006         if (sequence_control_set_ptr->static_config.tune == TUNE_SQ) {
3007             eb_vp9_signal_derivation_mode_decision_config_kernel_sq(
3008                 picture_control_set_ptr,
3009                 context_ptr);
3010         }
3011         else {
3012             eb_vp9_signal_derivation_mode_decision_config_kernel_oq_vmaf(
3013                 picture_control_set_ptr,
3014                 context_ptr);
3015         }
3016 
3017 #if VP9_RD
3018         // Initialize the rd cost
3019         // Hsan: should be done after QP generation (to clean up)
3020         cal_nmvjointsadcost(picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvjointsadcost);
3021         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost[0] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvcosts[0][MV_MAX];
3022         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost[1] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvcosts[1][MV_MAX];
3023         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost[0] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvsadcosts[0][MV_MAX];
3024         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost[1] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvsadcosts[1][MV_MAX];
3025         cal_nmvsadcosts(picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost);
3026 
3027         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost_hp[0] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvcosts_hp[0][MV_MAX];
3028         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost_hp[1] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvcosts_hp[1][MV_MAX];
3029         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost_hp[0] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvsadcosts_hp[0][MV_MAX];
3030         picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost_hp[1] = &picture_control_set_ptr->parent_pcs_ptr->cpi->nmvsadcosts_hp[1][MV_MAX];
3031         cal_nmvsadcosts_hp(picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost_hp);
3032 
3033         eb_vp9_initialize_rd_consts(picture_control_set_ptr->parent_pcs_ptr->cpi);
3034 
3035         if (picture_control_set_ptr->parent_pcs_ptr->cpi->common.allow_high_precision_mv) {
3036             picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.mvcost = picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost_hp;
3037             picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.mvsadcost = picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost_hp;
3038         }
3039         else {
3040             picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.mvcost = picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvcost;
3041             picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.mvsadcost = picture_control_set_ptr->parent_pcs_ptr->cpi->td.mb.nmvsadcost;
3042         }
3043 #endif
3044 
3045 #if SEG_SUPPORT
3046 #if BEA
3047         for (int segment_index = 0; segment_index < DELTA_QINDEX_SEGMENTS; ++segment_index) {
3048             context_ptr->qindex_delta[segment_index] = 0;
3049         }
3050         VP9_COMP *cpi = picture_control_set_ptr->parent_pcs_ptr->cpi;
3051         VP9_COMMON *const cm = &cpi->common;
3052         struct segmentation *const seg = &cm->seg;
3053 
3054         eb_vp9_disable_segmentation(seg);
3055         eb_vp9_clearall_segfeatures(seg);
3056 
3057         if (sequence_control_set_ptr->static_config.rate_control_mode == 2 && picture_control_set_ptr->temporal_layer_index < 1 && picture_control_set_ptr->parent_pcs_ptr->non_moving_average_score > 5) {
3058             qpm_derive_bea(
3059                 context_ptr,
3060                 picture_control_set_ptr,
3061                 sequence_control_set_ptr);
3062 
3063             eb_vp9_enable_segmentation(seg);
3064             // Select delta coding method.
3065             seg->abs_delta = SEGMENT_DELTADATA;
3066 
3067             // Use some of the segments for in frame Q adjustment.
3068             for (int segment = 0; segment < DELTA_QINDEX_SEGMENTS; ++segment) {
3069                 int qindex_delta;
3070 
3071                 qindex_delta = context_ptr->qindex_delta[segment];
3072                 //if ((cm->base_qindex + qindex_delta) > 0) {
3073                 if ((cm->base_qindex + qindex_delta) > 0 && picture_control_set_ptr->segment_counts[segment]) {
3074                     eb_vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
3075                     eb_vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
3076                 }
3077             }
3078         }
3079 #endif
3080 #endif
3081         picture_width_in_sb = (sequence_control_set_ptr->luma_width + MAX_SB_SIZE_MINUS_1) / MAX_SB_SIZE;
3082 
3083         context_ptr->qp = picture_control_set_ptr->picture_qp;
3084 
3085         picture_control_set_ptr->scene_characteristic_id = EB_FRAME_CARAC_0;
3086 
3087         EB_PICNOISE_CLASS pic_noise_classTH = (picture_control_set_ptr->parent_pcs_ptr->noise_detection_th == 0) ? PIC_NOISE_CLASS_1 : PIC_NOISE_CLASS_3;
3088 
3089         picture_control_set_ptr->scene_characteristic_id = (
3090             (!picture_control_set_ptr->parent_pcs_ptr->is_pan) &&
3091             (!picture_control_set_ptr->parent_pcs_ptr->is_tilt) &&
3092             (picture_control_set_ptr->parent_pcs_ptr->grass_percentage_in_picture > 0) &&
3093             (picture_control_set_ptr->parent_pcs_ptr->grass_percentage_in_picture <= 35) &&
3094             (picture_control_set_ptr->parent_pcs_ptr->pic_noise_class >= pic_noise_classTH) &&
3095             (picture_control_set_ptr->parent_pcs_ptr->pic_homogenous_over_time_sb_percentage < 50)) ? EB_FRAME_CARAC_1 : picture_control_set_ptr->scene_characteristic_id;
3096 
3097         picture_control_set_ptr->scene_characteristic_id = (
3098             (picture_control_set_ptr->parent_pcs_ptr->is_pan) &&
3099             (!picture_control_set_ptr->parent_pcs_ptr->is_tilt) &&
3100             (picture_control_set_ptr->parent_pcs_ptr->grass_percentage_in_picture > 35) &&
3101             (picture_control_set_ptr->parent_pcs_ptr->grass_percentage_in_picture <= 70) &&
3102             (picture_control_set_ptr->parent_pcs_ptr->pic_noise_class >= pic_noise_classTH) &&
3103             (picture_control_set_ptr->parent_pcs_ptr->pic_homogenous_over_time_sb_percentage < 50)) ? EB_FRAME_CARAC_2 : picture_control_set_ptr->scene_characteristic_id;
3104 
3105         // Aura Detection: uses the picture QP to derive aura thresholds, therefore it could not move to the open loop
3106         aura_detection(
3107             sequence_control_set_ptr,
3108             picture_control_set_ptr);
3109 
3110         // Detect complex/non-flat/moving LCU in a non-complex area (used to refine MDC depth control)
3111         complex_non_flat_moving_sb(
3112             sequence_control_set_ptr,
3113             picture_control_set_ptr,
3114             picture_width_in_sb);
3115 
3116         if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_SB_SWITCH_DEPTH_MODE) {
3117 
3118             eb_vp9_derive_sb_md_mode(
3119                 sequence_control_set_ptr,
3120                 picture_control_set_ptr,
3121                 context_ptr);
3122 
3123             for (int sb_index = 0; sb_index < picture_control_set_ptr->sb_total_count; ++sb_index) {
3124                 if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_FULL85_DEPTH_MODE) {
3125                     sb_depth_85_block(
3126                         sequence_control_set_ptr,
3127                         picture_control_set_ptr,
3128                         sb_index);
3129                 }
3130                 else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_FULL84_DEPTH_MODE) {
3131                     sb_depth_84_block(
3132                         sequence_control_set_ptr,
3133                         picture_control_set_ptr,
3134                         sb_index);
3135                 }
3136                 else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_AVC_DEPTH_MODE) {
3137                     sb_depth_8x8_16x16_block(
3138                         sequence_control_set_ptr,
3139                         picture_control_set_ptr,
3140                         sb_index);
3141                 }
3142                 else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_LIGHT_AVC_DEPTH_MODE) {
3143                     sb_depth_16x16_block(
3144                         sequence_control_set_ptr,
3145                         picture_control_set_ptr,
3146                         sb_index);
3147                 }
3148                 else if (picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_OPEN_LOOP_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_LIGHT_OPEN_LOOP_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_DEPTH_MODE || picture_control_set_ptr->parent_pcs_ptr->sb_depth_mode_array[sb_index] == SB_PRED_OPEN_LOOP_1_NFL_DEPTH_MODE) {
3149                     sb_depth_open_loop(
3150                         context_ptr,
3151                         sequence_control_set_ptr,
3152                         picture_control_set_ptr,
3153                         sb_index);
3154                 }
3155             }
3156         }
3157         else  if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_FULL85_DEPTH_MODE) {
3158             picture_depth_85_block(
3159                 sequence_control_set_ptr,
3160                 picture_control_set_ptr);
3161         }
3162         else  if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_FULL84_DEPTH_MODE) {
3163             picture_depth_84_block(
3164                 sequence_control_set_ptr,
3165                 picture_control_set_ptr);
3166         }
3167         else if (picture_control_set_ptr->parent_pcs_ptr->pic_depth_mode == PIC_OPEN_LOOP_DEPTH_MODE) {
3168             picture_depth_open_loop(
3169                 context_ptr,
3170                 sequence_control_set_ptr,
3171                 picture_control_set_ptr);
3172         }
3173 
3174         // Post the results to the MD processes
3175         eb_vp9_get_empty_object(
3176             context_ptr->mode_decision_configuration_output_fifo_ptr,
3177             &enc_dec_tasks_wrapper_ptr);
3178 
3179         enc_dec_tasks_ptr = (EncDecTasks*) enc_dec_tasks_wrapper_ptr->object_ptr;
3180         enc_dec_tasks_ptr->picture_control_set_wrapper_ptr = rate_control_results_ptr->picture_control_set_wrapper_ptr;
3181         enc_dec_tasks_ptr->input_type = ENCDEC_TASKS_MDC_INPUT;
3182 
3183         // Post the Full Results Object
3184         eb_vp9_post_full_object(enc_dec_tasks_wrapper_ptr);
3185 
3186         // Release Rate Control Results
3187         eb_vp9_release_object(rate_control_results_wrapper_ptr);
3188 
3189     }
3190 
3191     return EB_NULL;
3192 }
3193