1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11 */
12 
13 #include <stdlib.h>
14 
15 #include "EbEncHandle.h"
16 #include "EbUtility.h"
17 #include "EbPictureControlSet.h"
18 #include "EbModeDecisionConfigurationProcess.h"
19 #include "EbRateControlResults.h"
20 #include "EbEncDecTasks.h"
21 #include "EbReferenceObject.h"
22 #include "EbModeDecisionProcess.h"
23 #include "av1me.h"
24 #include "EbQMatrices.h"
25 #include "EbLog.h"
26 #include "EbCoefficients.h"
27 #include "EbCommonUtils.h"
28 #include "EbResize.h"
29 
30 int32_t get_qzbin_factor(int32_t q, AomBitDepth bit_depth);
31 void    invert_quant(int16_t *quant, int16_t *shift, int32_t d);
32 int16_t svt_av1_dc_quant_q3(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
33 int16_t svt_av1_ac_quant_q3(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
34 int16_t svt_av1_dc_quant_qtx(int32_t qindex, int32_t delta, AomBitDepth bit_depth);
35 
36 #define MAX_MESH_SPEED 5 // Max speed setting for mesh motion method
37 static MeshPattern good_quality_mesh_patterns[MAX_MESH_SPEED + 1][MAX_MESH_STEP] = {
38     {{64, 8}, {28, 4}, {15, 1}, {7, 1}},
39     {{64, 8}, {28, 4}, {15, 1}, {7, 1}},
40     {{64, 8}, {14, 2}, {7, 1}, {7, 1}},
41     {{64, 16}, {24, 8}, {12, 4}, {7, 1}},
42     {{64, 16}, {24, 8}, {12, 4}, {7, 1}},
43     {{64, 16}, {24, 8}, {12, 4}, {7, 1}},
44 };
45 static unsigned char good_quality_max_mesh_pct[MAX_MESH_SPEED + 1] = {50, 50, 25, 15, 5, 1};
46 // TODO: These settings are pretty relaxed, tune them for
47 // each speed setting
48 static MeshPattern intrabc_mesh_patterns[MAX_MESH_SPEED + 1][MAX_MESH_STEP] = {
49     {{256, 1}, {256, 1}, {0, 0}, {0, 0}},
50     {{256, 1}, {256, 1}, {0, 0}, {0, 0}},
51     {{64, 1}, {64, 1}, {0, 0}, {0, 0}},
52     {{64, 1}, {64, 1}, {0, 0}, {0, 0}},
53     {{64, 4}, {16, 1}, {0, 0}, {0, 0}},
54     {{64, 4}, {16, 1}, {0, 0}, {0, 0}},
55 };
56 static uint8_t intrabc_max_mesh_pct[MAX_MESH_SPEED + 1] = {100, 100, 100, 25, 25, 10};
57 
58 // Adaptive Depth Partitioning
59 // Shooting states
60 #define UNDER_SHOOTING 0
61 #define OVER_SHOOTING 1
62 #define TBD_SHOOTING 2
63 
64 #define SB_PRED_OPEN_LOOP_COST 100 // Let's assume PRED_OPEN_LOOP_COST costs ~100 U
65 #define U_101 101
66 #define U_102 102
67 #define U_103 103
68 #define U_104 104
69 #define U_105 105
70 #define U_107 107
71 #define SB_FAST_OPEN_LOOP_COST 108
72 #define U_109 109
73 #define SB_OPEN_LOOP_COST 110 // F_MDC is ~10% slower than PRED_OPEN_LOOP_COST
74 #define U_111 111
75 #define U_112 112
76 #define U_113 113
77 #define U_114 114
78 #define U_115 115
79 #define U_116 116
80 #define U_117 117
81 #define U_118 118
82 #define U_119 119
83 #define U_120 120
84 #define U_121 121
85 #define U_122 122
86 #define U_125 125
87 #define U_127 127
88 #define U_130 130
89 #define U_132 132
90 #define U_133 133
91 #define U_134 134
92 #define U_140 140
93 #define U_145 145
94 #define U_150 150
95 #define U_152 152
96 #define SQ_NON4_BLOCKS_SEARCH_COST 155
97 #define SQ_BLOCKS_SEARCH_COST 190
98 #define HIGH_SB_SCORE 60000
99 #define MEDIUM_SB_SCORE 16000
100 #define LOW_SB_SCORE 6000
101 #define MAX_LUMINOSITY_BOOST 10
102 int32_t budget_per_sb_boost[MAX_SUPPORTED_MODES] = {55, 55, 55, 55, 55, 55, 5, 5, 0};
103 
set_global_motion_field(PictureControlSet * pcs_ptr)104 void set_global_motion_field(PictureControlSet *pcs_ptr) {
105     // Init Global Motion Vector
106     uint8_t frame_index;
107     for (frame_index = INTRA_FRAME; frame_index <= ALTREF_FRAME; ++frame_index) {
108         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmtype   = IDENTITY;
109         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].alpha    = 0;
110         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].beta     = 0;
111         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].delta    = 0;
112         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].gamma    = 0;
113         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].invalid  = 0;
114         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[0] = 0;
115         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[1] = 0;
116         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[2] = (1 << WARPEDMODEL_PREC_BITS);
117         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[3] = 0;
118         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[4] = 0;
119         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[5] = (1 << WARPEDMODEL_PREC_BITS);
120         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[6] = 0;
121         pcs_ptr->parent_pcs_ptr->global_motion[frame_index].wmmat[7] = 0;
122     }
123 
124     //Update MV
125     PictureParentControlSet *parent_pcs_ptr = pcs_ptr->parent_pcs_ptr;
126     for (frame_index = INTRA_FRAME; frame_index <= ALTREF_FRAME; ++frame_index) {
127         if (parent_pcs_ptr
128                 ->is_global_motion[get_list_idx(frame_index)][get_ref_frame_idx(frame_index)])
129             parent_pcs_ptr->global_motion[frame_index] =
130                 parent_pcs_ptr->global_motion_estimation[get_list_idx(frame_index)]
131                                                         [get_ref_frame_idx(frame_index)];
132 
133         // Upscale the translation parameters by 2, because the search is done on a down-sampled
134         // version of the source picture (with a down-sampling factor of 2 in each dimension).
135         if (parent_pcs_ptr->gm_level == GM_DOWN16) {
136             parent_pcs_ptr->global_motion[frame_index].wmmat[0] *= 4;
137             parent_pcs_ptr->global_motion[frame_index].wmmat[1] *= 4;
138             parent_pcs_ptr->global_motion[frame_index].wmmat[0] = (int32_t)clamp(
139                 parent_pcs_ptr->global_motion[frame_index].wmmat[0],
140                 GM_TRANS_MIN * GM_TRANS_DECODE_FACTOR,
141                 GM_TRANS_MAX * GM_TRANS_DECODE_FACTOR);
142             parent_pcs_ptr->global_motion[frame_index].wmmat[1] = (int32_t)clamp(
143                 parent_pcs_ptr->global_motion[frame_index].wmmat[1],
144                 GM_TRANS_MIN * GM_TRANS_DECODE_FACTOR,
145                 GM_TRANS_MAX * GM_TRANS_DECODE_FACTOR);
146         } else if (parent_pcs_ptr->gm_level == GM_DOWN) {
147             parent_pcs_ptr->global_motion[frame_index].wmmat[0] *= 2;
148             parent_pcs_ptr->global_motion[frame_index].wmmat[1] *= 2;
149             parent_pcs_ptr->global_motion[frame_index].wmmat[0] = (int32_t)clamp(
150                 parent_pcs_ptr->global_motion[frame_index].wmmat[0],
151                 GM_TRANS_MIN * GM_TRANS_DECODE_FACTOR,
152                 GM_TRANS_MAX * GM_TRANS_DECODE_FACTOR);
153             parent_pcs_ptr->global_motion[frame_index].wmmat[1] = (int32_t)clamp(
154                 parent_pcs_ptr->global_motion[frame_index].wmmat[1],
155                 GM_TRANS_MIN * GM_TRANS_DECODE_FACTOR,
156                 GM_TRANS_MAX * GM_TRANS_DECODE_FACTOR);
157         }
158     }
159 }
160 
161 
svt_av1_build_quantizer(AomBitDepth bit_depth,int32_t y_dc_delta_q,int32_t u_dc_delta_q,int32_t u_ac_delta_q,int32_t v_dc_delta_q,int32_t v_ac_delta_q,Quants * const quants,Dequants * const deq)162 void svt_av1_build_quantizer(AomBitDepth bit_depth, int32_t y_dc_delta_q, int32_t u_dc_delta_q,
163                              int32_t u_ac_delta_q, int32_t v_dc_delta_q, int32_t v_ac_delta_q,
164                              Quants *const quants, Dequants *const deq) {
165     int32_t i, q, quant_q3, quant_qtx;
166 
167     for (q = 0; q < QINDEX_RANGE; q++) {
168         const int32_t qzbin_factor     = get_qzbin_factor(q, bit_depth);
169         const int32_t qrounding_factor = q == 0 ? 64 : 48;
170 
171         for (i = 0; i < 2; ++i) {
172             int32_t qrounding_factor_fp = 64;
173             // y quantizer setup with original coeff shift of Q3
174             quant_q3 = i == 0 ? svt_av1_dc_quant_q3(q, y_dc_delta_q, bit_depth)
175                               : svt_av1_ac_quant_q3(q, 0, bit_depth);
176             // y quantizer with TX scale
177             quant_qtx = i == 0 ? svt_av1_dc_quant_qtx(q, y_dc_delta_q, bit_depth)
178                                : svt_av1_ac_quant_qtx(q, 0, bit_depth);
179             invert_quant(&quants->y_quant[q][i], &quants->y_quant_shift[q][i], quant_qtx);
180             quants->y_quant_fp[q][i] = (int16_t)((1 << 16) / quant_qtx);
181             quants->y_round_fp[q][i] = (int16_t)((qrounding_factor_fp * quant_qtx) >> 7);
182             quants->y_zbin[q][i]     = (int16_t)ROUND_POWER_OF_TWO(qzbin_factor * quant_qtx, 7);
183             quants->y_round[q][i]    = (int16_t)((qrounding_factor * quant_qtx) >> 7);
184             deq->y_dequant_qtx[q][i] = (int16_t)quant_qtx;
185             deq->y_dequant_q3[q][i]  = (int16_t)quant_q3;
186 
187             // u quantizer setup with original coeff shift of Q3
188             quant_q3 = i == 0 ? svt_av1_dc_quant_q3(q, u_dc_delta_q, bit_depth)
189                               : svt_av1_ac_quant_q3(q, u_ac_delta_q, bit_depth);
190             // u quantizer with TX scale
191             quant_qtx = i == 0 ? svt_av1_dc_quant_qtx(q, u_dc_delta_q, bit_depth)
192                                : svt_av1_ac_quant_qtx(q, u_ac_delta_q, bit_depth);
193             invert_quant(&quants->u_quant[q][i], &quants->u_quant_shift[q][i], quant_qtx);
194             quants->u_quant_fp[q][i] = (int16_t)((1 << 16) / quant_qtx);
195             quants->u_round_fp[q][i] = (int16_t)((qrounding_factor_fp * quant_qtx) >> 7);
196             quants->u_zbin[q][i]     = (int16_t)ROUND_POWER_OF_TWO(qzbin_factor * quant_qtx, 7);
197             quants->u_round[q][i]    = (int16_t)((qrounding_factor * quant_qtx) >> 7);
198             deq->u_dequant_qtx[q][i] = (int16_t)quant_qtx;
199             deq->u_dequant_q3[q][i]  = (int16_t)quant_q3;
200 
201             // v quantizer setup with original coeff shift of Q3
202             quant_q3 = i == 0 ? svt_av1_dc_quant_q3(q, v_dc_delta_q, bit_depth)
203                               : svt_av1_ac_quant_q3(q, v_ac_delta_q, bit_depth);
204             // v quantizer with TX scale
205             quant_qtx = i == 0 ? svt_av1_dc_quant_qtx(q, v_dc_delta_q, bit_depth)
206                                : svt_av1_ac_quant_qtx(q, v_ac_delta_q, bit_depth);
207             invert_quant(&quants->v_quant[q][i], &quants->v_quant_shift[q][i], quant_qtx);
208             quants->v_quant_fp[q][i] = (int16_t)((1 << 16) / quant_qtx);
209             quants->v_round_fp[q][i] = (int16_t)((qrounding_factor_fp * quant_qtx) >> 7);
210             quants->v_zbin[q][i]     = (int16_t)ROUND_POWER_OF_TWO(qzbin_factor * quant_qtx, 7);
211             quants->v_round[q][i]    = (int16_t)((qrounding_factor * quant_qtx) >> 7);
212             deq->v_dequant_qtx[q][i] = (int16_t)quant_qtx;
213             deq->v_dequant_q3[q][i]  = (int16_t)quant_q3;
214         }
215 
216         for (i = 2; i < 8; i++) { // 8: SIMD width
217             quants->y_quant[q][i]       = quants->y_quant[q][1];
218             quants->y_quant_fp[q][i]    = quants->y_quant_fp[q][1];
219             quants->y_round_fp[q][i]    = quants->y_round_fp[q][1];
220             quants->y_quant_shift[q][i] = quants->y_quant_shift[q][1];
221             quants->y_zbin[q][i]        = quants->y_zbin[q][1];
222             quants->y_round[q][i]       = quants->y_round[q][1];
223             deq->y_dequant_qtx[q][i]    = deq->y_dequant_qtx[q][1];
224             deq->y_dequant_q3[q][i]     = deq->y_dequant_q3[q][1];
225 
226             quants->u_quant[q][i]       = quants->u_quant[q][1];
227             quants->u_quant_fp[q][i]    = quants->u_quant_fp[q][1];
228             quants->u_round_fp[q][i]    = quants->u_round_fp[q][1];
229             quants->u_quant_shift[q][i] = quants->u_quant_shift[q][1];
230             quants->u_zbin[q][i]        = quants->u_zbin[q][1];
231             quants->u_round[q][i]       = quants->u_round[q][1];
232             deq->u_dequant_qtx[q][i]    = deq->u_dequant_qtx[q][1];
233             deq->u_dequant_q3[q][i]     = deq->u_dequant_q3[q][1];
234             quants->v_quant[q][i]       = quants->u_quant[q][1];
235             quants->v_quant_fp[q][i]    = quants->v_quant_fp[q][1];
236             quants->v_round_fp[q][i]    = quants->v_round_fp[q][1];
237             quants->v_quant_shift[q][i] = quants->v_quant_shift[q][1];
238             quants->v_zbin[q][i]        = quants->v_zbin[q][1];
239             quants->v_round[q][i]       = quants->v_round[q][1];
240             deq->v_dequant_qtx[q][i]    = deq->v_dequant_qtx[q][1];
241             deq->v_dequant_q3[q][i]     = deq->v_dequant_q3[q][1];
242         }
243     }
244 }
245 
svt_av1_qm_init(PictureParentControlSet * pcs_ptr)246 void svt_av1_qm_init(PictureParentControlSet *pcs_ptr) {
247     const uint8_t num_planes = 3; // MAX_MB_PLANE;// NM- No monochroma
248     uint8_t       q, c, t;
249     int32_t       current;
250     for (q = 0; q < NUM_QM_LEVELS; ++q) {
251         for (c = 0; c < num_planes; ++c) {
252             current = 0;
253             for (t = 0; t < TX_SIZES_ALL; ++t) {
254                 const int32_t size       = tx_size_2d[t];
255                 const TxSize  qm_tx_size = av1_get_adjusted_tx_size(t);
256                 if (q == NUM_QM_LEVELS - 1) {
257                     pcs_ptr->gqmatrix[q][c][t]  = NULL;
258                     pcs_ptr->giqmatrix[q][c][t] = NULL;
259                 } else if (t != qm_tx_size) { // Reuse matrices for 'qm_tx_size'
260                     pcs_ptr->gqmatrix[q][c][t]  = pcs_ptr->gqmatrix[q][c][qm_tx_size];
261                     pcs_ptr->giqmatrix[q][c][t] = pcs_ptr->giqmatrix[q][c][qm_tx_size];
262                 } else {
263                     assert(current + size <= QM_TOTAL_SIZE);
264                     pcs_ptr->gqmatrix[q][c][t]  = &wt_matrix_ref[q][c >= 1][current];
265                     pcs_ptr->giqmatrix[q][c][t] = &iwt_matrix_ref[q][c >= 1][current];
266                     current += size;
267                 }
268             }
269         }
270     }
271 }
272 
273 /******************************************************
274 * Set the reference sg ep for a given picture
275 ******************************************************/
set_reference_sg_ep(PictureControlSet * pcs_ptr)276 void set_reference_sg_ep(PictureControlSet *pcs_ptr) {
277     Av1Common *        cm = pcs_ptr->parent_pcs_ptr->av1_cm;
278     EbReferenceObject *ref_obj_l0, *ref_obj_l1;
279     memset(cm->sg_frame_ep_cnt, 0, SGRPROJ_PARAMS * sizeof(int32_t));
280     cm->sg_frame_ep = 0;
281 
282     // NADER: set cm->sg_ref_frame_ep[0] = cm->sg_ref_frame_ep[1] = -1 to perform all iterations
283     switch (pcs_ptr->slice_type) {
284     case I_SLICE:
285         cm->sg_ref_frame_ep[0] = -1;
286         cm->sg_ref_frame_ep[1] = -1;
287         break;
288     case B_SLICE:
289         ref_obj_l0 = (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[REF_LIST_0][0]->object_ptr;
290         ref_obj_l1 = (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[REF_LIST_1][0]->object_ptr;
291         cm->sg_ref_frame_ep[0] = ref_obj_l0->sg_frame_ep;
292         cm->sg_ref_frame_ep[1] = ref_obj_l1->sg_frame_ep;
293         break;
294     case P_SLICE:
295         ref_obj_l0 = (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[REF_LIST_0][0]->object_ptr;
296         cm->sg_ref_frame_ep[0] = ref_obj_l0->sg_frame_ep;
297         cm->sg_ref_frame_ep[1] = 0;
298         break;
299     default: SVT_LOG("SG: Not supported picture type"); break;
300     }
301 }
302 
mode_decision_configuration_init_qp_update(PictureControlSet * pcs_ptr)303 void mode_decision_configuration_init_qp_update(PictureControlSet *pcs_ptr) {
304     FrameHeader *frm_hdr                = &pcs_ptr->parent_pcs_ptr->frm_hdr;
305     pcs_ptr->parent_pcs_ptr->average_qp = 0;
306     // Init block selection
307     // Set reference sg ep
308     set_reference_sg_ep(pcs_ptr);
309     set_global_motion_field(pcs_ptr);
310 
311     svt_av1_qm_init(pcs_ptr->parent_pcs_ptr);
312     MdRateEstimationContext *md_rate_estimation_array;
313 
314     md_rate_estimation_array = pcs_ptr->md_rate_estimation_array;
315 
316     if (pcs_ptr->parent_pcs_ptr->frm_hdr.primary_ref_frame != PRIMARY_REF_NONE)
317         memcpy(&pcs_ptr->md_frame_context,
318                &pcs_ptr->ref_frame_context[pcs_ptr->parent_pcs_ptr->frm_hdr.primary_ref_frame],
319                sizeof(FRAME_CONTEXT));
320     else {
321         svt_av1_default_coef_probs(&pcs_ptr->md_frame_context,
322                                    frm_hdr->quantization_params.base_q_idx);
323         init_mode_probs(&pcs_ptr->md_frame_context);
324     }
325     // Initial Rate Estimation of the syntax elements
326     av1_estimate_syntax_rate(md_rate_estimation_array,
327                              pcs_ptr->slice_type == I_SLICE ? EB_TRUE : EB_FALSE,
328                              &pcs_ptr->md_frame_context);
329     // Initial Rate Estimation of the Motion vectors
330     av1_estimate_mv_rate(pcs_ptr, md_rate_estimation_array, &pcs_ptr->md_frame_context);
331     // Initial Rate Estimation of the quantized coefficients
332     av1_estimate_coefficients_rate(md_rate_estimation_array, &pcs_ptr->md_frame_context);
333 }
334 
335 /******************************************************
336 * Compute Tc, and Beta offsets for a given picture
337 ******************************************************/
338 
mode_decision_configuration_context_dctor(EbPtr p)339 static void mode_decision_configuration_context_dctor(EbPtr p) {
340     EbThreadContext *                 thread_context_ptr = (EbThreadContext *)p;
341     ModeDecisionConfigurationContext *obj                = (ModeDecisionConfigurationContext *)
342                                                 thread_context_ptr->priv;
343 
344     if (obj->is_md_rate_estimation_ptr_owner)
345         EB_FREE_ARRAY(obj->md_rate_estimation_ptr);
346     EB_FREE_ARRAY(obj);
347 }
348 /******************************************************
349  * Mode Decision Configuration Context Constructor
350  ******************************************************/
mode_decision_configuration_context_ctor(EbThreadContext * thread_context_ptr,const EbEncHandle * enc_handle_ptr,int input_index,int output_index)351 EbErrorType mode_decision_configuration_context_ctor(EbThreadContext *  thread_context_ptr,
352                                                      const EbEncHandle *enc_handle_ptr,
353                                                      int input_index, int output_index) {
354     ModeDecisionConfigurationContext *context_ptr;
355     EB_CALLOC_ARRAY(context_ptr, 1);
356     thread_context_ptr->priv  = context_ptr;
357     thread_context_ptr->dctor = mode_decision_configuration_context_dctor;
358 
359     // Input/Output System Resource Manager FIFOs
360     context_ptr->rate_control_input_fifo_ptr = svt_system_resource_get_consumer_fifo(
361         enc_handle_ptr->rate_control_results_resource_ptr, input_index);
362     context_ptr->mode_decision_configuration_output_fifo_ptr =
363         svt_system_resource_get_producer_fifo(enc_handle_ptr->enc_dec_tasks_resource_ptr,
364                                               output_index);
365     // Rate estimation
366     EB_MALLOC_ARRAY(context_ptr->md_rate_estimation_ptr, 1);
367     context_ptr->is_md_rate_estimation_ptr_owner = EB_TRUE;
368     return EB_ErrorNone;
369 }
370 
set_cdf_controls(PictureControlSet * pcs,uint8_t update_cdf_level)371 void set_cdf_controls(PictureControlSet *pcs, uint8_t update_cdf_level) {
372     CdfControls *ctrl = &pcs->cdf_ctrl;
373     switch (update_cdf_level) {
374     case 0:
375         ctrl->update_mv   = 0;
376         ctrl->update_se   = 0;
377         ctrl->update_coef = 0;
378         break;
379     case 1:
380         ctrl->update_mv   = 1;
381         ctrl->update_se   = 1;
382         ctrl->update_coef = 1;
383         break;
384     case 2:
385         ctrl->update_mv   = 0;
386         ctrl->update_se   = 1;
387         ctrl->update_coef = 1;
388         break;
389     case 3:
390         ctrl->update_mv   = 0;
391         ctrl->update_se   = 1;
392         ctrl->update_coef = 0;
393         break;
394     default: assert(0); break;
395     }
396 
397     ctrl->update_mv = pcs->slice_type == I_SLICE ? 0 : ctrl->update_mv;
398     ctrl->enabled   = ctrl->update_coef | ctrl->update_mv | ctrl->update_se;
399 }
400 /******************************************************
401 * Derive Mode Decision Config Settings for OQ
402 Input   : encoder mode and tune
403 Output  : EncDec Kernel signal(s)
404 ******************************************************/
signal_derivation_mode_decision_config_kernel_oq(SequenceControlSet * scs_ptr,PictureControlSet * pcs_ptr)405 EbErrorType signal_derivation_mode_decision_config_kernel_oq(SequenceControlSet *scs_ptr,
406                                                              PictureControlSet * pcs_ptr) {
407     UNUSED(scs_ptr);
408     EbErrorType return_error = EB_ErrorNone;
409 
410     uint8_t update_cdf_level = 0;
411     if (pcs_ptr->enc_mode <= ENC_M2)
412         update_cdf_level = 1;
413     else if (pcs_ptr->enc_mode <= ENC_M4)
414         update_cdf_level = pcs_ptr->slice_type == I_SLICE
415         ? 1
416         : (pcs_ptr->temporal_layer_index == 0) ? 1 : 3;
417     else if (pcs_ptr->enc_mode <= ENC_M6)
418         update_cdf_level = pcs_ptr->slice_type == I_SLICE
419         ? 1
420         : 3;
421     else if (pcs_ptr->enc_mode <= ENC_M8)
422         update_cdf_level = pcs_ptr->slice_type == I_SLICE ? 1 : 0;
423     else
424         update_cdf_level = 0;
425 
426     //set the conrols uisng the required level
427     set_cdf_controls(pcs_ptr, update_cdf_level);
428     //Filter Intra Mode : 0: OFF  1: ON
429     // pic_filter_intra_level specifies whether filter intra would be active
430     // for a given picture.
431 
432     // pic_filter_intra_level | Settings
433     // 0                      | OFF
434     // 1                      | ON
435     if (scs_ptr->static_config.filter_intra_level == DEFAULT) {
436         if (scs_ptr->seq_header.filter_intra_level) {
437             if (pcs_ptr->enc_mode <= ENC_M5)
438                 pcs_ptr->pic_filter_intra_level = 1;
439             else
440                 pcs_ptr->pic_filter_intra_level = 0;
441         } else
442             pcs_ptr->pic_filter_intra_level = 0;
443     } else
444         pcs_ptr->pic_filter_intra_level = scs_ptr->static_config.filter_intra_level;
445     FrameHeader *frm_hdr             = &pcs_ptr->parent_pcs_ptr->frm_hdr;
446     frm_hdr->allow_high_precision_mv = frm_hdr->quantization_params.base_q_idx <
447                 HIGH_PRECISION_MV_QTHRESH &&
448             (scs_ptr->input_resolution <= INPUT_SIZE_480p_RANGE)
449         ? 1
450         : 0;
451     EbBool enable_wm;
452     if (pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M3) {
453         enable_wm = EB_TRUE;
454     } else if (pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M8) {
455         enable_wm = (pcs_ptr->parent_pcs_ptr->temporal_layer_index == 0) ? EB_TRUE : EB_FALSE;
456     } else {
457         enable_wm = EB_FALSE;
458     }
459     if (pcs_ptr->parent_pcs_ptr->scs_ptr->static_config.enable_warped_motion != DEFAULT)
460         enable_wm = (EbBool)pcs_ptr->parent_pcs_ptr->scs_ptr->static_config.enable_warped_motion;
461 
462     // Note: local warp should be disabled when super-res is ON
463     // according to the AV1 spec 5.11.27
464     frm_hdr->allow_warped_motion = enable_wm &&
465         !(frm_hdr->frame_type == KEY_FRAME || frm_hdr->frame_type == INTRA_ONLY_FRAME) &&
466         !frm_hdr->error_resilient_mode && !pcs_ptr->parent_pcs_ptr->frame_superres_enabled;
467 
468     frm_hdr->is_motion_mode_switchable = frm_hdr->allow_warped_motion;
469 
470     // pic_obmc_level - pic_obmc_level is used to define md_pic_obmc_level.
471     // The latter determines the OBMC settings in the function set_obmc_controls.
472     // Please check the definitions of the flags/variables in the function
473     // set_obmc_controls corresponding to the pic_obmc_level settings.
474 
475     //  pic_obmc_level  |              Default Encoder Settings             |     Command Line Settings
476     //         0        | OFF subject to possible constraints               | OFF everywhere in encoder
477     //         1        | ON subject to possible constraints                | Fully ON in PD_PASS_2
478     //         2        | Faster level subject to possible constraints      | Level 2 everywhere in PD_PASS_2
479     //         3        | Even faster level subject to possible constraints | Level 3 everywhere in PD_PASS_3
480     if (scs_ptr->static_config.obmc_level == DEFAULT) {
481         if (pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M3)
482             pcs_ptr->parent_pcs_ptr->pic_obmc_level = 1;
483         else if (pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M5)
484             pcs_ptr->parent_pcs_ptr->pic_obmc_level = 2;
485         else if (pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M7)
486             pcs_ptr->parent_pcs_ptr->pic_obmc_level = pcs_ptr->parent_pcs_ptr->is_used_as_reference_flag ? 2 : 0;
487         else
488             pcs_ptr->parent_pcs_ptr->pic_obmc_level = 0;
489     } else
490         pcs_ptr->parent_pcs_ptr->pic_obmc_level = scs_ptr->static_config.obmc_level;
491 
492     // Switchable Motion Mode
493     frm_hdr->is_motion_mode_switchable = frm_hdr->is_motion_mode_switchable ||
494         pcs_ptr->parent_pcs_ptr->pic_obmc_level;
495 
496     pcs_ptr->parent_pcs_ptr->bypass_cost_table_gen = 0;
497     if(scs_ptr->input_resolution <= INPUT_SIZE_480p_RANGE)
498          pcs_ptr->parent_pcs_ptr->bypass_cost_table_gen = 0;
499     else if(pcs_ptr->parent_pcs_ptr->enc_mode <= ENC_M7)
500         pcs_ptr->parent_pcs_ptr->bypass_cost_table_gen = 0;
501     //else if(pcs_ptr->slice_type == I_SLICE)
502     else if(pcs_ptr->picture_number == 0)
503         pcs_ptr->parent_pcs_ptr->bypass_cost_table_gen = 0;
504     else
505         pcs_ptr->parent_pcs_ptr->bypass_cost_table_gen = 1;
506     return return_error;
507 }
508 
509 /******************************************************
510 * Derive Mode Decision Config Settings for first pass
511 Input   : encoder mode and tune
512 Output  : EncDec Kernel signal(s)
513 ******************************************************/
514 EbErrorType first_pass_signal_derivation_mode_decision_config_kernel(PictureControlSet *pcs_ptr);
515 void        av1_set_ref_frame(MvReferenceFrame *rf, int8_t ref_frame_type);
516 
get_relative_dist(const OrderHintInfo * oh,int a,int b)517 static INLINE int get_relative_dist(const OrderHintInfo *oh, int a, int b) {
518     if (!oh->enable_order_hint)
519         return 0;
520 
521     const int bits = oh->order_hint_bits;
522 
523     assert(bits >= 1);
524     assert(a >= 0 && a < (1 << bits));
525     assert(b >= 0 && b < (1 << bits));
526 
527     int       diff = a - b;
528     const int m    = 1 << (bits - 1);
529     diff           = (diff & (m - 1)) - (diff & m);
530     return diff;
531 }
532 
get_block_position(Av1Common * cm,int * mi_r,int * mi_c,int blk_row,int blk_col,MV mv,int sign_bias)533 static int get_block_position(Av1Common *cm, int *mi_r, int *mi_c, int blk_row, int blk_col, MV mv,
534                               int sign_bias) {
535     const int base_blk_row = (blk_row >> 3) << 3;
536     const int base_blk_col = (blk_col >> 3) << 3;
537 
538     const int row_offset = (mv.row >= 0) ? (mv.row >> (4 + MI_SIZE_LOG2))
539                                          : -((-mv.row) >> (4 + MI_SIZE_LOG2));
540 
541     const int col_offset = (mv.col >= 0) ? (mv.col >> (4 + MI_SIZE_LOG2))
542                                          : -((-mv.col) >> (4 + MI_SIZE_LOG2));
543 
544     const int row = (sign_bias == 1) ? blk_row - row_offset : blk_row + row_offset;
545     const int col = (sign_bias == 1) ? blk_col - col_offset : blk_col + col_offset;
546 
547     if (row < 0 || row >= (cm->mi_rows >> 1) || col < 0 || col >= (cm->mi_cols >> 1))
548         return 0;
549 
550     if (row < base_blk_row - (MAX_OFFSET_HEIGHT >> 3) ||
551         row >= base_blk_row + 8 + (MAX_OFFSET_HEIGHT >> 3) ||
552         col < base_blk_col - (MAX_OFFSET_WIDTH >> 3) ||
553         col >= base_blk_col + 8 + (MAX_OFFSET_WIDTH >> 3))
554         return 0;
555 
556     *mi_r = row;
557     *mi_c = col;
558 
559     return 1;
560 }
561 
562 #define MFMV_STACK_SIZE 3
563 
564 // Note: motion_filed_projection finds motion vectors of current frame's
565 // reference frame, and projects them to current frame. To make it clear,
566 // let's call current frame's reference frame as start frame.
567 // Call Start frame's reference frames as reference frames.
568 // Call ref_offset as frame distances between start frame and its reference
569 // frames.
motion_field_projection(Av1Common * cm,PictureControlSet * pcs_ptr,MvReferenceFrame start_frame,int dir)570 static int motion_field_projection(Av1Common *cm, PictureControlSet *pcs_ptr,
571                                    MvReferenceFrame start_frame, int dir) {
572     TPL_MV_REF *tpl_mvs_base           = pcs_ptr->tpl_mvs;
573     int         ref_offset[REF_FRAMES] = {0};
574 
575     uint8_t list_idx0, ref_idx_l0;
576     list_idx0  = get_list_idx(start_frame);
577     ref_idx_l0 = get_ref_frame_idx(start_frame);
578     EbReferenceObject *start_frame_buf =
579         (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[list_idx0][ref_idx_l0]->object_ptr;
580 
581     if (start_frame_buf == NULL)
582         return 0;
583 
584     if (start_frame_buf->frame_type == KEY_FRAME || start_frame_buf->frame_type == INTRA_ONLY_FRAME)
585         return 0;
586 
587     // MFMV is not applied when the reference picture is of a different spatial resolution
588     // (described in the AV1 spec section 7.9.2.)
589     if (start_frame_buf->mi_rows != cm->mi_rows || start_frame_buf->mi_cols != cm->mi_cols) {
590         return 0;
591     }
592 
593     const int                 start_frame_order_hint        = start_frame_buf->order_hint;
594     const unsigned int *const ref_order_hints               = &start_frame_buf->ref_order_hint[0];
595     int                       start_to_current_frame_offset = get_relative_dist(
596         &pcs_ptr->parent_pcs_ptr->scs_ptr->seq_header.order_hint_info,
597         start_frame_order_hint,
598         pcs_ptr->parent_pcs_ptr->cur_order_hint);
599 
600     for (int i = LAST_FRAME; i <= INTER_REFS_PER_FRAME; ++i)
601         ref_offset[i] = get_relative_dist(
602             &pcs_ptr->parent_pcs_ptr->scs_ptr->seq_header.order_hint_info,
603             start_frame_order_hint,
604             ref_order_hints[i - LAST_FRAME]);
605 
606     if (dir == 2)
607         start_to_current_frame_offset = -start_to_current_frame_offset;
608 
609     const MV_REF *const mv_ref_base = start_frame_buf->mvs;
610     const int           mvs_rows    = (cm->mi_rows + 1) >> 1;
611     const int           mvs_cols    = (cm->mi_cols + 1) >> 1;
612 
613     for (int blk_row = 0; blk_row < mvs_rows; ++blk_row) {
614         for (int blk_col = 0; blk_col < mvs_cols; ++blk_col) {
615             const MV_REF *const mv_ref = &mv_ref_base[blk_row * mvs_cols + blk_col];
616             MV                  fwd_mv = mv_ref->mv.as_mv;
617 
618             if (mv_ref->ref_frame > INTRA_FRAME) {
619                 MV        this_mv;
620                 int       mi_r, mi_c;
621                 const int ref_frame_offset = ref_offset[mv_ref->ref_frame];
622 
623                 int pos_valid = abs(ref_frame_offset) <= MAX_FRAME_DISTANCE &&
624                     ref_frame_offset > 0 &&
625                     abs(start_to_current_frame_offset) <= MAX_FRAME_DISTANCE;
626 
627                 if (pos_valid) {
628                     get_mv_projection(
629                         &this_mv, fwd_mv, start_to_current_frame_offset, ref_frame_offset);
630                     pos_valid = get_block_position(
631                         cm, &mi_r, &mi_c, blk_row, blk_col, this_mv, dir >> 1);
632                 }
633 
634                 if (pos_valid) {
635                     const int mi_offset = mi_r * (cm->mi_stride >> 1) + mi_c;
636 
637                     tpl_mvs_base[mi_offset].mfmv0.as_mv.row  = fwd_mv.row;
638                     tpl_mvs_base[mi_offset].mfmv0.as_mv.col  = fwd_mv.col;
639                     tpl_mvs_base[mi_offset].ref_frame_offset = ref_frame_offset;
640                 }
641             }
642         }
643     }
644 
645     return 1;
646 }
av1_setup_motion_field(Av1Common * cm,PictureControlSet * pcs_ptr)647 static void av1_setup_motion_field(Av1Common *cm, PictureControlSet *pcs_ptr) {
648     const OrderHintInfo *const order_hint_info =
649         &pcs_ptr->parent_pcs_ptr->scs_ptr->seq_header.order_hint_info;
650     memset(pcs_ptr->ref_frame_side, 0, sizeof(pcs_ptr->ref_frame_side));
651     if (!order_hint_info->enable_order_hint)
652         return;
653 
654     TPL_MV_REF *tpl_mvs_base = pcs_ptr->tpl_mvs;
655     int         size         = ((cm->mi_rows + MAX_MIB_SIZE) >> 1) * (cm->mi_stride >> 1);
656 
657     const int                cur_order_hint = pcs_ptr->parent_pcs_ptr->cur_order_hint;
658     const EbReferenceObject *ref_buf[INTER_REFS_PER_FRAME];
659     int                      ref_order_hint[INTER_REFS_PER_FRAME];
660 
661     for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
662         const int ref_idx    = ref_frame - LAST_FRAME;
663         int       order_hint = 0;
664         uint8_t   list_idx0, ref_idx_l0;
665         list_idx0  = get_list_idx(ref_frame);
666         ref_idx_l0 = get_ref_frame_idx(ref_frame);
667         EbReferenceObject *buf =
668             (EbReferenceObject *)pcs_ptr->ref_pic_ptr_array[list_idx0][ref_idx_l0]->object_ptr;
669 
670         if (buf != NULL)
671             order_hint = buf->order_hint;
672 
673         ref_buf[ref_idx]        = buf;
674         ref_order_hint[ref_idx] = order_hint;
675 
676         if (get_relative_dist(order_hint_info, order_hint, cur_order_hint) > 0)
677             pcs_ptr->ref_frame_side[ref_frame] = 1;
678         else if (order_hint == cur_order_hint)
679             pcs_ptr->ref_frame_side[ref_frame] = -1;
680     }
681 
682     //for a frame based mfmv, we need to keep computing the ref_frame_side regardless mfmv is used or no
683     if (!pcs_ptr->parent_pcs_ptr->frm_hdr.use_ref_frame_mvs)
684         return;
685 
686 
687     for (int idx = 0; idx < size; ++idx) {
688         tpl_mvs_base[idx].mfmv0.as_int = INVALID_MV;
689         tpl_mvs_base[idx].ref_frame_offset = 0;
690 
691     }
692 
693 
694 
695     int ref_stamp = MFMV_STACK_SIZE - 1;
696 
697     if (ref_buf[LAST_FRAME - LAST_FRAME] != NULL) {
698         const int alt_of_lst_order_hint =
699             ref_buf[LAST_FRAME - LAST_FRAME]->ref_order_hint[ALTREF_FRAME - LAST_FRAME];
700         const int is_lst_overlay = (alt_of_lst_order_hint ==
701                                     ref_order_hint[GOLDEN_FRAME - LAST_FRAME]);
702         if (!is_lst_overlay)
703             motion_field_projection(cm, pcs_ptr, LAST_FRAME, 2);
704 
705         --ref_stamp;
706     }
707 
708     if (get_relative_dist(
709             order_hint_info, ref_order_hint[BWDREF_FRAME - LAST_FRAME], cur_order_hint) > 0) {
710         if (motion_field_projection(cm, pcs_ptr, BWDREF_FRAME, 0))
711             --ref_stamp;
712     }
713 
714         if (get_relative_dist(
715                 order_hint_info, ref_order_hint[ALTREF2_FRAME - LAST_FRAME], cur_order_hint) > 0) {
716             if (motion_field_projection(cm, pcs_ptr, ALTREF2_FRAME, 0))
717                 --ref_stamp;
718         }
719 
720     if (get_relative_dist(
721             order_hint_info, ref_order_hint[ALTREF_FRAME - LAST_FRAME], cur_order_hint) > 0 &&
722         ref_stamp >= 0)
723         if (motion_field_projection(cm, pcs_ptr, ALTREF_FRAME, 0))
724             --ref_stamp;
725 
726     if (ref_stamp >= 0)
727         motion_field_projection(cm, pcs_ptr, LAST2_FRAME, 2);
728 }
729 
730 /* Mode Decision Configuration Kernel */
731 
732 /*********************************************************************************
733 *
734 * @brief
735 *  The Mode Decision Configuration Process involves a number of initialization steps,
736 *  setting flags for a number of features, and determining the blocks to be considered
737 *  in subsequent MD stages.
738 *
739 * @par Description:
740 *  The Mode Decision Configuration Process involves a number of initialization steps,
741 *  setting flags for a number of features, and determining the blocks to be considered
742 *  in subsequent MD stages. Examples of flags that are set are the flags for filter intra,
743 *  eighth-pel, OBMC and warped motion and flags for updating the cumulative density functions
744 *  Examples of initializations include initializations for picture chroma QP offsets,
745 *  CDEF strength, self-guided restoration filter parameters, quantization parameters,
746 *  lambda arrays, mv and coefficient rate estimation arrays.
747 *
748 *  The set of blocks to be processed in subsequent MD stages is decided in this process as a
749 *  function of the picture depth mode (pic_depth_mode).
750 *
751 * @param[in] Configurations
752 *  Configuration flags that are to be set
753 *
754 * @param[out] Initializations
755 *  Initializations for various flags and variables
756 *
757 ********************************************************************************/
mode_decision_configuration_kernel(void * input_ptr)758 void *mode_decision_configuration_kernel(void *input_ptr) {
759     // Context & SCS & PCS
760     EbThreadContext *                 thread_context_ptr = (EbThreadContext *)input_ptr;
761     ModeDecisionConfigurationContext *context_ptr        = (ModeDecisionConfigurationContext *)
762                                                         thread_context_ptr->priv;
763     // Input
764     EbObjectWrapper *rate_control_results_wrapper_ptr;
765 
766     // Output
767     EbObjectWrapper *enc_dec_tasks_wrapper_ptr;
768 
769     for (;;) {
770         // Get RateControl Results
771         EB_GET_FULL_OBJECT(context_ptr->rate_control_input_fifo_ptr,
772                            &rate_control_results_wrapper_ptr);
773 
774         RateControlResults *rate_control_results_ptr =
775             (RateControlResults *)rate_control_results_wrapper_ptr->object_ptr;
776         PictureControlSet *pcs_ptr = (PictureControlSet *)
777                                          rate_control_results_ptr->pcs_wrapper_ptr->object_ptr;
778         SequenceControlSet *scs_ptr = (SequenceControlSet *)pcs_ptr->scs_wrapper_ptr->object_ptr;
779 
780         // -------
781         // Scale references if resolution of the reference is different than the input
782         // -------
783         if (pcs_ptr->parent_pcs_ptr->frame_superres_enabled == 1 &&
784             pcs_ptr->slice_type != I_SLICE) {
785             if (pcs_ptr->parent_pcs_ptr->is_used_as_reference_flag == EB_TRUE &&
786                 pcs_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr != NULL) {
787                 // update mi_rows and mi_cols for the reference pic wrapper (used in mfmv for other pictures)
788                 EbReferenceObject *reference_object =
789                     pcs_ptr->parent_pcs_ptr->reference_picture_wrapper_ptr->object_ptr;
790                 reference_object->mi_rows = pcs_ptr->parent_pcs_ptr->aligned_height >> MI_SIZE_LOG2;
791                 reference_object->mi_cols = pcs_ptr->parent_pcs_ptr->aligned_width >> MI_SIZE_LOG2;
792             }
793 
794             scale_rec_references(
795                 pcs_ptr, pcs_ptr->parent_pcs_ptr->enhanced_picture_ptr, pcs_ptr->hbd_mode_decision);
796         }
797 
798 
799 
800         if (pcs_ptr->slice_type != I_SLICE && scs_ptr->mfmv_enabled)
801             av1_setup_motion_field(pcs_ptr->parent_pcs_ptr->av1_cm, pcs_ptr);
802 
803         FrameHeader *frm_hdr = &pcs_ptr->parent_pcs_ptr->frm_hdr;
804 
805         // Mode Decision Configuration Kernel Signal(s) derivation
806         if (use_output_stat(scs_ptr))
807             first_pass_signal_derivation_mode_decision_config_kernel(pcs_ptr);
808         else
809             signal_derivation_mode_decision_config_kernel_oq(scs_ptr, pcs_ptr);
810 
811         pcs_ptr->parent_pcs_ptr->average_qp = 0;
812         // Init block selection
813         // Set reference sg ep
814         set_reference_sg_ep(pcs_ptr);
815         set_global_motion_field(pcs_ptr);
816 
817         svt_av1_qm_init(pcs_ptr->parent_pcs_ptr);
818         MdRateEstimationContext *md_rate_estimation_array;
819 
820         // QP
821         context_ptr->qp = pcs_ptr->picture_qp;
822 
823         // QP Index
824         context_ptr->qp_index = (uint8_t)frm_hdr->quantization_params.base_q_idx;
825 
826         md_rate_estimation_array = pcs_ptr->md_rate_estimation_array;
827         // Reset MD rate Estimation table to initial values by copying from md_rate_estimation_array
828         if (context_ptr->is_md_rate_estimation_ptr_owner) {
829             EB_FREE_ARRAY(context_ptr->md_rate_estimation_ptr);
830             context_ptr->is_md_rate_estimation_ptr_owner = EB_FALSE;
831         }
832         context_ptr->md_rate_estimation_ptr = md_rate_estimation_array;
833         if (pcs_ptr->parent_pcs_ptr->frm_hdr.primary_ref_frame != PRIMARY_REF_NONE)
834             memcpy(&pcs_ptr->md_frame_context,
835                    &pcs_ptr->ref_frame_context[pcs_ptr->parent_pcs_ptr->frm_hdr.primary_ref_frame],
836                    sizeof(FRAME_CONTEXT));
837         else {
838             svt_av1_default_coef_probs(&pcs_ptr->md_frame_context,
839                                        frm_hdr->quantization_params.base_q_idx);
840             init_mode_probs(&pcs_ptr->md_frame_context);
841         }
842         // Initial Rate Estimation of the syntax elements
843         av1_estimate_syntax_rate(md_rate_estimation_array,
844                                  pcs_ptr->slice_type == I_SLICE ? EB_TRUE : EB_FALSE,
845                                  &pcs_ptr->md_frame_context);
846         // Initial Rate Estimation of the Motion vectors
847         if (!use_output_stat(scs_ptr)){
848         av1_estimate_mv_rate(pcs_ptr, md_rate_estimation_array, &pcs_ptr->md_frame_context);
849         // Initial Rate Estimation of the quantized coefficients
850         av1_estimate_coefficients_rate(md_rate_estimation_array, &pcs_ptr->md_frame_context);
851         }
852         if (frm_hdr->allow_intrabc) {
853             int            i;
854             int            speed          = 1;
855             SpeedFeatures *sf             = &pcs_ptr->sf;
856             sf->allow_exhaustive_searches = 1;
857 
858             const int mesh_speed = AOMMIN(speed, MAX_MESH_SPEED);
859             //if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION)
860             //    sf->exhaustive_searches_thresh = (1 << 24);
861             //else
862             sf->exhaustive_searches_thresh = (1 << 25);
863 
864             sf->max_exaustive_pct = good_quality_max_mesh_pct[mesh_speed];
865             if (mesh_speed > 0)
866                 sf->exhaustive_searches_thresh = sf->exhaustive_searches_thresh << 1;
867 
868             for (i = 0; i < MAX_MESH_STEP; ++i) {
869                 sf->mesh_patterns[i].range    = good_quality_mesh_patterns[mesh_speed][i].range;
870                 sf->mesh_patterns[i].interval = good_quality_mesh_patterns[mesh_speed][i].interval;
871             }
872 
873             if (pcs_ptr->slice_type == I_SLICE) {
874                 for (i = 0; i < MAX_MESH_STEP; ++i) {
875                     sf->mesh_patterns[i].range    = intrabc_mesh_patterns[mesh_speed][i].range;
876                     sf->mesh_patterns[i].interval = intrabc_mesh_patterns[mesh_speed][i].interval;
877                 }
878                 sf->max_exaustive_pct = intrabc_max_mesh_pct[mesh_speed];
879             }
880 
881             {
882                 // add to hash table
883                 const int pic_width  = pcs_ptr->parent_pcs_ptr->aligned_width;
884                 const int pic_height = pcs_ptr->parent_pcs_ptr->aligned_height;
885 
886                 uint32_t *block_hash_values[2][2];
887                 int8_t *  is_block_same[2][3];
888                 int       k, j;
889 
890                 for (k = 0; k < 2; k++) {
891                     for (j = 0; j < 2; j++)
892                         block_hash_values[k][j] = malloc(sizeof(uint32_t) * pic_width * pic_height);
893                     for (j = 0; j < 3; j++)
894                         is_block_same[k][j] = malloc(sizeof(int8_t) * pic_width * pic_height);
895                 }
896 
897                 //pcs_ptr->hash_table.p_lookup_table = NULL;
898                 //svt_av1_hash_table_create(&pcs_ptr->hash_table);
899 
900                 Yv12BufferConfig cpi_source;
901                 link_eb_to_aom_buffer_desc_8bit(pcs_ptr->parent_pcs_ptr->enhanced_picture_ptr,
902                                                 &cpi_source);
903 
904                 svt_av1_crc_calculator_init(&pcs_ptr->crc_calculator1, 24, 0x5D6DCB);
905                 svt_av1_crc_calculator_init(&pcs_ptr->crc_calculator2, 24, 0x864CFB);
906 
907                 svt_av1_generate_block_2x2_hash_value(
908                     &cpi_source, block_hash_values[0], is_block_same[0], pcs_ptr);
909                 svt_av1_generate_block_hash_value(&cpi_source,
910                                                   4,
911                                                   block_hash_values[0],
912                                                   block_hash_values[1],
913                                                   is_block_same[0],
914                                                   is_block_same[1],
915                                                   pcs_ptr);
916                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
917                                                                 block_hash_values[1],
918                                                                 is_block_same[1][2],
919                                                                 pic_width,
920                                                                 pic_height,
921                                                                 4);
922                 svt_av1_generate_block_hash_value(&cpi_source,
923                                                   8,
924                                                   block_hash_values[1],
925                                                   block_hash_values[0],
926                                                   is_block_same[1],
927                                                   is_block_same[0],
928                                                   pcs_ptr);
929                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
930                                                                 block_hash_values[0],
931                                                                 is_block_same[0][2],
932                                                                 pic_width,
933                                                                 pic_height,
934                                                                 8);
935                 svt_av1_generate_block_hash_value(&cpi_source,
936                                                   16,
937                                                   block_hash_values[0],
938                                                   block_hash_values[1],
939                                                   is_block_same[0],
940                                                   is_block_same[1],
941                                                   pcs_ptr);
942                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
943                                                                 block_hash_values[1],
944                                                                 is_block_same[1][2],
945                                                                 pic_width,
946                                                                 pic_height,
947                                                                 16);
948                 svt_av1_generate_block_hash_value(&cpi_source,
949                                                   32,
950                                                   block_hash_values[1],
951                                                   block_hash_values[0],
952                                                   is_block_same[1],
953                                                   is_block_same[0],
954                                                   pcs_ptr);
955                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
956                                                                 block_hash_values[0],
957                                                                 is_block_same[0][2],
958                                                                 pic_width,
959                                                                 pic_height,
960                                                                 32);
961                 svt_av1_generate_block_hash_value(&cpi_source,
962                                                   64,
963                                                   block_hash_values[0],
964                                                   block_hash_values[1],
965                                                   is_block_same[0],
966                                                   is_block_same[1],
967                                                   pcs_ptr);
968                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
969                                                                 block_hash_values[1],
970                                                                 is_block_same[1][2],
971                                                                 pic_width,
972                                                                 pic_height,
973                                                                 64);
974 
975                 svt_av1_generate_block_hash_value(&cpi_source,
976                                                   128,
977                                                   block_hash_values[1],
978                                                   block_hash_values[0],
979                                                   is_block_same[1],
980                                                   is_block_same[0],
981                                                   pcs_ptr);
982                 svt_av1_add_to_hash_map_by_row_with_precal_data(&pcs_ptr->hash_table,
983                                                                 block_hash_values[0],
984                                                                 is_block_same[0][2],
985                                                                 pic_width,
986                                                                 pic_height,
987                                                                 128);
988 
989                 for (k = 0; k < 2; k++) {
990                     for (j = 0; j < 2; j++) free(block_hash_values[k][j]);
991                     for (j = 0; j < 3; j++) free(is_block_same[k][j]);
992                 }
993             }
994 
995             svt_av1_init3smotion_compensation(
996                 &pcs_ptr->ss_cfg, pcs_ptr->parent_pcs_ptr->enhanced_picture_ptr->stride_y);
997         }
998 
999         // Post the results to the MD processes
1000 
1001         uint16_t tg_count = pcs_ptr->parent_pcs_ptr->tile_group_cols *
1002             pcs_ptr->parent_pcs_ptr->tile_group_rows;
1003         for (uint16_t tile_group_idx = 0; tile_group_idx < tg_count; tile_group_idx++) {
1004             svt_get_empty_object(context_ptr->mode_decision_configuration_output_fifo_ptr,
1005                                  &enc_dec_tasks_wrapper_ptr);
1006 
1007             EncDecTasks *enc_dec_tasks_ptr = (EncDecTasks *)enc_dec_tasks_wrapper_ptr->object_ptr;
1008             enc_dec_tasks_ptr->pcs_wrapper_ptr  = rate_control_results_ptr->pcs_wrapper_ptr;
1009             enc_dec_tasks_ptr->input_type       = ENCDEC_TASKS_MDC_INPUT;
1010             enc_dec_tasks_ptr->tile_group_index = tile_group_idx;
1011 
1012             // Post the Full Results Object
1013             svt_post_full_object(enc_dec_tasks_wrapper_ptr);
1014         }
1015 
1016         // Release Rate Control Results
1017         svt_release_object(rate_control_results_wrapper_ptr);
1018     }
1019 
1020     return NULL;
1021 }
1022