LCOV - code coverage report
Current view: top level - Codec - EbModeDecision.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2404 2863 84.0 %
Date: 2019-11-25 17:12:20 Functions: 42 50 84.0 %

          Line data    Source code
       1             : /*
       2             : * Copyright(c) 2019 Intel Corporation
       3             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       4             : */
       5             : 
       6             : /*
       7             : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
       8             : *
       9             : * This source code is subject to the terms of the BSD 2 Clause License and
      10             : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      11             : * was not distributed with this source code in the LICENSE file, you can
      12             : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      13             : * Media Patent License 1.0 was not distributed with this source code in the
      14             : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
      15             : */
      16             : 
      17             : /***************************************
      18             : * Includes
      19             : ***************************************/
      20             : #include <stdlib.h>
      21             : 
      22             : #include "EbDefinitions.h"
      23             : #include "EbUtility.h"
      24             : #include "EbCommonUtils.h"
      25             : #include "EbSequenceControlSet.h"
      26             : 
      27             : #include "EbModeDecision.h"
      28             : #include "EbAdaptiveMotionVectorPrediction.h"
      29             : #include "EbTransformUnit.h"
      30             : #include "EbModeDecisionProcess.h"
      31             : #include "EbMotionEstimation.h"
      32             : #include "EbAdaptiveMotionVectorPrediction.h"
      33             : 
      34             : #include "av1me.h"
      35             : #include "hash.h"
      36             : #include "EbInterPrediction.h"
      37             : #if II_COMP_FLAG
      38             : #include "EbRateDistortionCost.h"
      39             : #endif
      40             : #include "aom_dsp_rtcd.h"
      41             : #define  INCRMENT_CAND_TOTAL_COUNT(cnt) MULTI_LINE_MACRO_BEGIN cnt++; if(cnt>=MODE_DECISION_CANDIDATE_MAX_COUNT) printf(" ERROR: reaching limit for MODE_DECISION_CANDIDATE_MAX_COUNT %i\n",cnt); MULTI_LINE_MACRO_END
      42             : 
      43             : int8_t av1_ref_frame_type(const MvReferenceFrame *const rf);
      44             : 
      45             : #if FILTER_INTRA_FLAG
      46             : int av1_filter_intra_allowed_bsize(uint8_t enable_filter_intra, BlockSize bs);
      47             : #endif
      48             : #if OBMC_FLAG
      49             : #define INT_MAX       2147483647    // maximum (signed) int value
      50             : #endif
      51             : 
      52             : static EB_AV1_INTER_PREDICTION_FUNC_PTR   av1_inter_prediction_function_table[2] =
      53             : {
      54             :     av1_inter_prediction,
      55             :     av1_inter_prediction_hbd
      56             : };
      57             : 
      58             : #if II_COMP_FLAG
      59             : void av1_set_ref_frame(MvReferenceFrame *rf,
      60             :     int8_t ref_frame_type);
      61             : 
      62             : 
      63   111100000 : static INLINE int is_interintra_allowed_bsize(const BlockSize bsize) {
      64   111100000 :     return (bsize >= BLOCK_8X8) && (bsize <= BLOCK_32X32);
      65             : }
      66             : 
      67    81501500 : static INLINE int is_interintra_allowed_mode(const PredictionMode mode) {
      68    81501500 :     return (mode >= SINGLE_INTER_MODE_START) && (mode < SINGLE_INTER_MODE_END);
      69             : }
      70             : 
      71    25490700 : static INLINE int is_interintra_allowed_ref(const MvReferenceFrame rf[2]) {
      72    25490700 :     return (rf[0] > INTRA_FRAME) && (rf[1] <= INTRA_FRAME);
      73             : }
      74             : 
      75             : 
      76             : 
      77   111587000 : int svt_is_interintra_allowed(
      78             :     uint8_t enable_inter_intra,
      79             :     BlockSize sb_type,
      80             :     PredictionMode mode,
      81             :     MvReferenceFrame ref_frame[2])
      82             : {
      83             :     return
      84   111151000 :         enable_inter_intra &&
      85   192666000 :         is_interintra_allowed_bsize((const BlockSize)sb_type) &&
      86   304410000 :         is_interintra_allowed_mode(mode)  &&
      87    25322300 :         is_interintra_allowed_ref(ref_frame);
      88             : 
      89             : }
      90             : #endif
      91             : /********************************************
      92             : * Constants
      93             : ********************************************/
      94             : #if II_COMP_FLAG
      95             : // 1 - Regular uni-pred ,
      96             : // 2 - Regular uni-pred + Wedge compound Inter Intra
      97             : // 3 - Regular uni-pred + Wedge compound Inter Intra + Smooth compound Inter Intra
      98             : 
      99             : #define II_COUNT                3
     100             : #endif
     101             : #if OBMC_FLAG
     102             : MotionMode obmc_motion_mode_allowed(
     103             :     const PictureControlSet       *picture_control_set_ptr,
     104             :     const CodingUnit              *cu_ptr,
     105             :     const BlockSize                 bsize,
     106             :     MvReferenceFrame                rf0,
     107             :     MvReferenceFrame                rf1,
     108             :     PredictionMode                  mode);
     109             : 
     110             : void precompute_obmc_data(
     111             :     PictureControlSet            *picture_control_set_ptr,
     112             :     ModeDecisionContext          *context_ptr);
     113             : #endif
     114             : //static uint32_t  AntiContouringIntraMode[11] = { EB_INTRA_PLANAR, EB_INTRA_DC, EB_INTRA_HORIZONTAL, EB_INTRA_VERTICAL,
     115             : //EB_INTRA_MODE_2, EB_INTRA_MODE_6, EB_INTRA_MODE_14, EB_INTRA_MODE_18, EB_INTRA_MODE_22, EB_INTRA_MODE_30, EB_INTRA_MODE_34 };
     116           0 : static int32_t have_newmv_in_inter_mode(PredictionMode mode) {
     117           0 :     return (mode == NEWMV || mode == NEW_NEWMV || mode == NEAREST_NEWMV ||
     118           0 :         mode == NEW_NEARESTMV || mode == NEAR_NEWMV || mode == NEW_NEARMV);
     119             : }
     120             : const uint32_t parentIndex[85] = { 0, 0, 0, 2, 2, 2, 2, 0, 7, 7, 7, 7, 0, 12, 12, 12, 12, 0, 17, 17, 17, 17, 0, 0,
     121             : 23, 23, 23, 23, 0, 28, 28, 28, 28, 0, 33, 33, 33, 33, 0, 38, 38, 38, 38, 0, 0,
     122             : 44, 44, 44, 44, 0, 49, 49, 49, 49, 0, 54, 54, 54, 54, 0, 59, 59, 59, 59, 0, 0,
     123             : 65, 65, 65, 65, 0, 70, 70, 70, 70, 0, 75, 75, 75, 75, 0, 80, 80, 80, 80 };
     124             : /*
     125             :   NORMAL ORDER
     126             :   |-------------------------------------------------------------|
     127             :   | ref_idx          0            1           2            3       |
     128             :   | List0            LAST        LAST2        LAST3        GOLD    |
     129             :   | List1            BWD            ALT2        ALT                 |
     130             :   |-------------------------------------------------------------|
     131             : */
     132             : #define INVALID_REF 0xF
     133             : 
     134   546364000 : uint8_t get_list_idx(uint8_t ref_type) {
     135   546364000 :     if (ref_type == LAST_FRAME || ref_type == LAST2_FRAME || ref_type == LAST3_FRAME || ref_type == GOLDEN_FRAME)
     136   323971000 :         return 0;
     137   222393000 :     else if (ref_type == BWDREF_FRAME || ref_type == ALTREF_FRAME || ref_type == ALTREF2_FRAME)
     138   222393000 :         return 1;
     139             :     else
     140           0 :         return (INVALID_REF);
     141             : };
     142             : 
     143    76379300 : uint8_t get_ref_frame_idx(uint8_t ref_type) {
     144    76379300 :     if (ref_type == LAST_FRAME || ref_type == BWDREF_FRAME)
     145    32998400 :         return 0;
     146    43380900 :     else if (ref_type == LAST2_FRAME || ref_type == ALTREF2_FRAME)
     147    20472600 :         return 1;
     148    22908300 :     else if (ref_type == LAST3_FRAME || ref_type == ALTREF_FRAME)
     149    15620800 :         return 2;
     150     7287470 :     else if (ref_type == GOLDEN_FRAME)
     151     6232190 :         return 3;
     152             :     else
     153     1055280 :         return (INVALID_REF);
     154             : };
     155   147527000 : MvReferenceFrame svt_get_ref_frame_type(uint8_t list, uint8_t ref_idx) {
     156   147527000 :     switch (list) {
     157    88942000 :     case 0:
     158    88942000 :         return (ref_idx == 0 ? LAST_FRAME : ref_idx == 1 ? LAST2_FRAME : ref_idx == 2 ? LAST3_FRAME : ref_idx == 3 ? GOLDEN_FRAME : INVALID_REF);
     159    58763100 :     case 1:
     160    58763100 :         return (ref_idx == 0 ? BWDREF_FRAME : ref_idx == 1 ? ALTREF2_FRAME : ref_idx == 2 ? ALTREF_FRAME : INVALID_REF);
     161           0 :     default:
     162           0 :         return (INVALID_REF);
     163             :     }
     164             : };
     165             : extern uint32_t stage1ModesArray[];
     166             : 
     167             : uint8_t GetMaxDrlIndex(uint8_t  refmvCnt, PredictionMode   mode);
     168             : int32_t eb_av1_mv_bit_cost(const MV *mv, const MV *ref, const int32_t *mvjcost,
     169             :     int32_t *mvcost[2], int32_t weight);
     170             : #define MV_COST_WEIGHT 108
     171             : 
     172             : #if II_COMP_FLAG
     173             : #define MAX_INTERINTRA_SB_SQUARE 32 * 32
     174             : 
     175             : EbErrorType  intra_luma_prediction_for_interintra(
     176             :     ModeDecisionContext         *md_context_ptr,
     177             :     PictureControlSet           *picture_control_set_ptr,
     178             :     INTERINTRA_MODE              interintra_mode,
     179             :     EbPictureBufferDesc         *prediction_ptr);
     180             : int64_t pick_wedge_fixed_sign(
     181             :     ModeDecisionCandidate        *candidate_ptr,
     182             :     PictureControlSet                    *picture_control_set_ptr,
     183             :     ModeDecisionContext                  *context_ptr,
     184             :     const BlockSize bsize,
     185             :     const int16_t *const residual1,
     186             :     const int16_t *const diff10,
     187             :     const int8_t wedge_sign,
     188             :     int8_t *const best_wedge_index);
     189             : void model_rd_for_sb_with_curvfit(
     190             :     PictureControlSet      *picture_control_set_ptr,
     191             :     ModeDecisionContext                  *context_ptr,
     192             :     BlockSize bsize, int bw, int bh,
     193             :     uint8_t* src_buf, uint32_t src_stride, uint8_t* pred_buf, uint32_t pred_stride,
     194             :     int plane_from, int plane_to, int mi_row, int mi_col, int *out_rate_sum,
     195             :     int64_t *out_dist_sum, int *skip_txfm_sb, int64_t *skip_sse_sb,
     196             :     int *plane_rate, int64_t *plane_sse, int64_t *plane_dist);
     197             : 
     198     4453290 : static int64_t pick_interintra_wedge(
     199             :     ModeDecisionCandidate        *candidate_ptr,
     200             :     PictureControlSet                    *picture_control_set_ptr,
     201             :     ModeDecisionContext                  *context_ptr,
     202             :     const BlockSize bsize,
     203             :     const uint8_t *const p0,
     204             :     const uint8_t *const p1,
     205             :     uint8_t * src_buf,
     206             :     uint32_t  src_stride,
     207             :     int32_t *wedge_index_out
     208             :     )
     209             : {
     210             : 
     211     4453290 :     assert(is_interintra_wedge_used(bsize));
     212             :    // assert(cpi->common.seq_params.enable_interintra_compound);
     213             : 
     214     4453290 :     const int bw = block_size_wide[bsize];
     215     4453290 :     const int bh = block_size_high[bsize];
     216             :     DECLARE_ALIGNED(32, int16_t, residual1[MAX_SB_SQUARE]);  // src - pred1
     217             :     DECLARE_ALIGNED(32, int16_t, diff10[MAX_SB_SQUARE]);     // pred1 - pred0
     218             : 
     219             :     {
     220     4453290 :         aom_subtract_block(bh, bw, residual1, bw, src_buf, src_stride, p1, bw);
     221     4453520 :         aom_subtract_block(bh, bw, diff10, bw, p1, bw, p0, bw);
     222             :     }
     223             : 
     224     4453520 :     int8_t wedge_index = -1;
     225             :     int64_t rd =
     226     4453520 :         pick_wedge_fixed_sign(candidate_ptr,picture_control_set_ptr, context_ptr, bsize, residual1, diff10, 0, &wedge_index);
     227             : 
     228     4453510 :     *wedge_index_out = wedge_index;
     229             : 
     230     4453510 :     return rd;
     231             : }
     232             : #if II_COMP_FLAG
     233             : //for every CU, perform DC/V/H/S intra prediction to be used later in inter-intra search
     234      295931 : void precompute_intra_pred_for_inter_intra(
     235             :     PictureControlSet            *picture_control_set_ptr,
     236             :     ModeDecisionContext          *context_ptr)
     237             : {
     238             :     uint32_t j;
     239             :     EbPictureBufferDesc  pred_desc;
     240      295931 :     pred_desc.origin_x = pred_desc.origin_y = 0;
     241      295931 :     pred_desc.stride_y = context_ptr->blk_geom->bwidth;
     242             : 
     243     1479640 :     for (j = 0; j < INTERINTRA_MODES; ++j)
     244             :     {
     245             : 
     246     1183710 :         INTERINTRA_MODE interintra_mode = (INTERINTRA_MODE)j;
     247     1183710 :         pred_desc.buffer_y = context_ptr->intrapred_buf[j];
     248     1183710 :         intra_luma_prediction_for_interintra(
     249             :             context_ptr,
     250             :             picture_control_set_ptr,
     251             :             interintra_mode,
     252             :             &pred_desc);
     253             :     }
     254      295929 : }
     255             : #endif
     256             :  void combine_interintra(INTERINTRA_MODE mode,
     257             :     int8_t use_wedge_interintra, int wedge_index,
     258             :     int wedge_sign, BlockSize bsize,
     259             :     BlockSize plane_bsize, uint8_t *comppred,
     260             :     int compstride, const uint8_t *interpred,
     261             :     int interstride, const uint8_t *intrapred,
     262             :     int intrastride);
     263     4453720 : void inter_intra_search(
     264             :     PictureControlSet            *picture_control_set_ptr,
     265             :     ModeDecisionContext          *context_ptr,
     266             :     ModeDecisionCandidate        *candidate_ptr)
     267             : {
     268             :     DECLARE_ALIGNED(16, uint8_t, tmp_buf[ MAX_INTERINTRA_SB_SQUARE]);
     269             :     DECLARE_ALIGNED(16, uint8_t, intrapred[ MAX_INTERINTRA_SB_SQUARE]);
     270             : 
     271             :     DECLARE_ALIGNED(16, uint8_t, ii_pred_buf[MAX_INTERINTRA_SB_SQUARE]);
     272             :     //get inter pred for ref0
     273     4453720 :     EbPictureBufferDesc   *src_pic = picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr;
     274     4453720 :     uint8_t               *src_buf = src_pic->buffer_y + (context_ptr->cu_origin_x + src_pic->origin_x) + (context_ptr->cu_origin_y + src_pic->origin_y) * src_pic->stride_y;
     275             : 
     276     4453720 :     uint32_t  bwidth = context_ptr->blk_geom->bwidth;
     277     4453720 :     uint32_t  bheight = context_ptr->blk_geom->bheight;
     278             :     EbPictureBufferDesc  pred_desc;
     279     4453720 :     pred_desc.origin_x = pred_desc.origin_y = 0;
     280     4453720 :     pred_desc.stride_y = bwidth;
     281             : 
     282             :     EbPictureBufferDesc  *ref_pic_list0;
     283     4453720 :     EbPictureBufferDesc  *ref_pic_list1 = NULL;
     284             :     Mv mv_0;
     285             :     Mv mv_1;
     286     4453720 :     mv_0.x = candidate_ptr->motion_vector_xl0;
     287     4453720 :     mv_0.y = candidate_ptr->motion_vector_yl0;
     288     4453720 :     mv_1.x = candidate_ptr->motion_vector_xl1;
     289     4453720 :     mv_1.y = candidate_ptr->motion_vector_yl1;
     290             :     MvUnit mv_unit;
     291     4453720 :     mv_unit.mv[0] = mv_0;
     292     4453720 :     mv_unit.mv[1] = mv_1;
     293     4453720 :     int8_t ref_idx_l0 = candidate_ptr->ref_frame_index_l0;
     294     4453720 :     int8_t ref_idx_l1 = candidate_ptr->ref_frame_index_l1;
     295             :     MvReferenceFrame rf[2];
     296     4453720 :     av1_set_ref_frame(rf, candidate_ptr->ref_frame_type);
     297             :     uint8_t list_idx0, list_idx1;
     298     4453690 :     list_idx0 = get_list_idx(rf[0]);
     299     4453700 :     if (rf[1] == NONE_FRAME)
     300     4453700 :         list_idx1 = get_list_idx(rf[0]);
     301             :     else
     302           0 :         list_idx1 = get_list_idx(rf[1]);
     303     4453550 :     assert(list_idx0 < MAX_NUM_OF_REF_PIC_LIST);
     304     4453550 :     assert(list_idx1 < MAX_NUM_OF_REF_PIC_LIST);
     305     4453550 :     if (ref_idx_l0 >= 0)
     306     2825860 :         ref_pic_list0 = ((EbReferenceObject*)picture_control_set_ptr->ref_pic_ptr_array[list_idx0][ref_idx_l0]->object_ptr)->reference_picture;
     307             :     else
     308     1627680 :         ref_pic_list0 = (EbPictureBufferDesc*)EB_NULL;
     309     4453550 :     if (ref_idx_l1 >= 0)
     310     1627940 :         ref_pic_list1 = ((EbReferenceObject*)picture_control_set_ptr->ref_pic_ptr_array[list_idx1][ref_idx_l1]->object_ptr)->reference_picture;
     311             :     else
     312     2825600 :         ref_pic_list1 = (EbPictureBufferDesc*)EB_NULL;
     313             : 
     314     4453550 :     mv_unit.pred_direction = candidate_ptr->prediction_direction[0];
     315             : 
     316     4453550 :     pred_desc.buffer_y = tmp_buf;
     317             : 
     318             :     //we call the regular inter prediction path here(no compound)
     319     4453550 :     av1_inter_prediction_function_table[context_ptr->hbd_mode_decision > EB_8_BIT_MD](
     320             :         picture_control_set_ptr,
     321             :         0,//ASSUMPTION: fixed interpolation filter.
     322             :         context_ptr->cu_ptr,
     323     4453550 :         candidate_ptr->ref_frame_type,
     324             :         &mv_unit,
     325             :         0,//use_intrabc,
     326             : #if OBMC_FLAG
     327             :         SIMPLE_TRANSLATION,
     328             :         0,
     329             :         0,
     330             : #endif
     331             :         1,//compound_idx not used
     332             :         NULL,// interinter_comp not used
     333             : #if II_COMP_FLAG
     334             :         NULL,
     335             :         NULL,
     336             :         NULL,
     337             :         NULL,
     338             :         0,
     339             :         0,
     340             :         0,
     341             :         0,
     342             : #endif
     343     4453550 :         context_ptr->cu_origin_x,
     344     4453550 :         context_ptr->cu_origin_y,
     345             :         bwidth,
     346             :         bheight,
     347             :         ref_pic_list0,
     348             :         ref_pic_list1,
     349             :         &pred_desc, //output
     350             :         0,          //output origin_x,
     351             :         0,          //output origin_y,
     352             :         0, //do chroma
     353     4453550 :         (uint8_t)picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_ptr->static_config.encoder_bit_depth);
     354             : 
     355     4453320 :     assert(is_interintra_wedge_used(context_ptr->blk_geom->bsize));//if not I need to add nowedge path!!
     356             : 
     357     4453280 :     int64_t rd = INT64_MAX;
     358     4453280 :     int64_t best_interintra_rd = INT64_MAX;
     359     4453280 :     int rmode = 0, rate_sum;
     360             :     int64_t dist_sum;
     361     4453280 :     int tmp_rate_mv = 0;
     362             : 
     363             : 
     364     4453280 :     INTERINTRA_MODE best_interintra_mode = INTERINTRA_MODES;
     365             :     EbPictureBufferDesc  inra_pred_desc;
     366     4453280 :     inra_pred_desc.origin_x = inra_pred_desc.origin_y = 0;
     367     4453280 :     inra_pred_desc.stride_y = bwidth;
     368     4453280 :     inra_pred_desc.buffer_y = intrapred;
     369             : 
     370     4453280 :     int8_t enable_smooth_interintra =1;
     371             :       //if (cpi->oxcf.enable_smooth_interintra &&
     372             :       //!cpi->sf.disable_smooth_interintra) {
     373     4453280 :     if (enable_smooth_interintra) {
     374     4453150 :         int j = 0;
     375     4453150 :         if (/*cpi->sf.reuse_inter_intra_mode == 0 ||*/
     376             :             best_interintra_mode == INTERINTRA_MODES) {
     377    22264100 :             for (j = 0; j < INTERINTRA_MODES; ++j) {
     378             :                 //if ((!cpi->oxcf.enable_smooth_intra || cpi->sf.disable_smooth_intra) &&
     379             :                 //    (INTERINTRA_MODE)j == II_SMOOTH_PRED)
     380             :                 //  continue;
     381    17806700 :                 INTERINTRA_MODE interintra_mode = (INTERINTRA_MODE)j;
     382             :                 //rmode = interintra_mode_cost[mbmi->interintra_mode];
     383    17806700 :                 const int bsize_group = size_group_lookup[context_ptr->blk_geom->bsize];
     384    17806700 :                 rmode  = candidate_ptr->md_rate_estimation_ptr->inter_intra_mode_fac_bits[bsize_group][interintra_mode];
     385             : #if !II_COMP_FLAG
     386             :                 inra_pred_desc.buffer_y = context_ptr->intrapred_buf[j];
     387             : 
     388             :                 //av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
     389             :                 //                                          intrapred, bw);
     390             :                 intra_luma_prediction_for_interintra(
     391             :                     context_ptr,
     392             :                     picture_control_set_ptr,
     393             :                     interintra_mode,
     394             :                     &inra_pred_desc);
     395             : #endif
     396             : 
     397             :                 //av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
     398    17806700 :                 combine_interintra(
     399             :                     interintra_mode,//mode,
     400             :                     0,//use_wedge_interintra,
     401             :                     0,//candidate_ptr->interintra_wedge_index,
     402             :                     0,//int wedge_sign,
     403    17806700 :                     context_ptr->blk_geom->bsize,
     404    17806700 :                     context_ptr->blk_geom->bsize,// plane_bsize,
     405             :                     ii_pred_buf, bwidth, /*uint8_t *comppred, int compstride,*/
     406             :                     tmp_buf, bwidth,  /*const uint8_t *interpred, int interstride,*/
     407    17806700 :                     context_ptr->intrapred_buf[j], bwidth /*const uint8_t *intrapred,   int intrastride*/);
     408             : 
     409             :                 //model_rd_sb_fn[MODELRD_TYPE_INTERINTRA](
     410             :                 //    cpi, bsize, x, xd, 0, 0, mi_row, mi_col, &rate_sum, &dist_sum,
     411             :                 //    &tmp_skip_txfm_sb, &tmp_skip_sse_sb, NULL, NULL, NULL);
     412    17809600 :                 model_rd_for_sb_with_curvfit(picture_control_set_ptr, context_ptr, context_ptr->blk_geom->bsize, bwidth, bheight,
     413    17809600 :                     src_buf, src_pic->stride_y, ii_pred_buf, bwidth,
     414             :                     0, 0, 0, 0, &rate_sum, &dist_sum, NULL, NULL, NULL, NULL, NULL);
     415             : 
     416             :                 // rd = RDCOST(x->rdmult, tmp_rate_mv + rate_sum + rmode, dist_sum);
     417    17811000 :                 rd = RDCOST(context_ptr->full_lambda, tmp_rate_mv + rate_sum + rmode, dist_sum);
     418             : 
     419    17811000 :                 if (rd < best_interintra_rd) {
     420    10542500 :                     best_interintra_rd = rd;
     421    10542500 :                     candidate_ptr->interintra_mode = best_interintra_mode = interintra_mode;
     422             :                 }
     423             :             }
     424             : 
     425             :             /* best_interintra_rd_wedge =
     426             :                  pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);*/
     427             : 
     428             :             //CHKN need to re-do intra pred using the winner, or have a separate intra serch for wedge
     429             : 
     430     4457440 :             pick_interintra_wedge(
     431             :                 candidate_ptr,
     432             :                 picture_control_set_ptr,
     433             :                 context_ptr,
     434     4457440 :                 context_ptr->blk_geom->bsize,
     435             : #if II_COMP_FLAG
     436     4457440 :                 context_ptr->intrapred_buf[best_interintra_mode],
     437             : #else
     438             :                 intrapred,
     439             : #endif
     440             :                 tmp_buf,
     441             :                 src_buf,
     442     4457440 :                 src_pic->stride_y,
     443             :                 &candidate_ptr->interintra_wedge_index
     444             :             );
     445             : 
     446             :             //if (best_interintra_rd_wedge < best_interintra_rd) {
     447             : 
     448             :                 //candidate_ptr->use_wedge_interintra = 1;
     449             :                 //candidate_ptr->ii_wedge_sign = 0;
     450             :             //}
     451             :             //args->inter_intra_mode[mbmi->ref_frame[0]] = best_interintra_mode;
     452             :         }
     453             :     }
     454             :     // Enable wedge search if source variance and edge strength are above the thresholds.
     455     4453670 :     int enable_wedge_interintra_search = 0;
     456     4453670 :     if (enable_wedge_interintra_search)
     457             :     {
     458           0 :         if (best_interintra_mode == INTERINTRA_MODES) {//Optimization TBD: search only for the first MV mode per ref
     459             : 
     460           0 :             INTERINTRA_MODE   interintra_mode = II_SMOOTH_PRED;
     461           0 :             intra_luma_prediction_for_interintra(
     462             :                 context_ptr,
     463             :                 picture_control_set_ptr,
     464             :                 interintra_mode,
     465             :                 &inra_pred_desc);
     466             : 
     467           0 :             candidate_ptr->ii_wedge_sign = 0;
     468             : 
     469           0 :             pick_interintra_wedge(
     470             :                 candidate_ptr,
     471             :                 picture_control_set_ptr,
     472             :                 context_ptr,
     473           0 :                 context_ptr->blk_geom->bsize,
     474             :                 intrapred,
     475             :                 tmp_buf,
     476             :                 src_buf,
     477           0 :                 src_pic->stride_y,
     478             :                 &candidate_ptr->interintra_wedge_index
     479             :                 );
     480             : 
     481           0 :             int j = 0;
     482           0 :             for (j = 0; j < INTERINTRA_MODES; ++j) {
     483             : 
     484           0 :                 interintra_mode = (INTERINTRA_MODE)j;
     485             : 
     486           0 :                 const int bsize_group = size_group_lookup[context_ptr->blk_geom->bsize];
     487           0 :                 rmode  = candidate_ptr->md_rate_estimation_ptr->inter_intra_mode_fac_bits[bsize_group][interintra_mode];
     488             : 
     489           0 :                 intra_luma_prediction_for_interintra(
     490             :                     context_ptr,
     491             :                     picture_control_set_ptr,
     492             :                     interintra_mode,
     493             :                     &inra_pred_desc);
     494             : 
     495           0 :                 combine_interintra(
     496             :                     interintra_mode,//mode,
     497             :                     1,//use_wedge_interintra,
     498             :                     candidate_ptr->interintra_wedge_index,
     499             :                     0,//int wedge_sign,
     500           0 :                     context_ptr->blk_geom->bsize,
     501           0 :                     context_ptr->blk_geom->bsize,// plane_bsize,
     502             :                     ii_pred_buf, bwidth, /*uint8_t *comppred, int compstride,*/
     503             :                     tmp_buf, bwidth,  /*const uint8_t *interpred, int interstride,*/
     504             :                     intrapred, bwidth /*const uint8_t *intrapred,   int intrastride*/);
     505             : 
     506             : 
     507           0 :                 model_rd_for_sb_with_curvfit(picture_control_set_ptr, context_ptr, context_ptr->blk_geom->bsize, bwidth, bheight,
     508           0 :                     src_buf, src_pic->stride_y, ii_pred_buf, bwidth,
     509             :                    0, 0, 0, 0, &rate_sum,  &dist_sum, NULL, NULL, NULL, NULL, NULL);
     510             : 
     511           0 :                 rd = RDCOST(context_ptr->full_lambda, tmp_rate_mv + rate_sum + rmode, dist_sum);
     512           0 :                 if (rd < best_interintra_rd) {
     513           0 :                     best_interintra_mode = interintra_mode;
     514             : 
     515             :                     //CHKN added this as fix from lib-aom
     516           0 :                     best_interintra_rd = rd;
     517             :                 }
     518             :             }
     519             : 
     520           0 :             candidate_ptr->interintra_mode      = best_interintra_mode;
     521           0 :             candidate_ptr->use_wedge_interintra =1 ;
     522             :         }
     523             :     }
     524             : 
     525             : 
     526     4453670 : }
     527             : #endif
     528             : 
     529             : COMPOUND_TYPE to_av1_compound_lut[] = {
     530             :     COMPOUND_AVERAGE,
     531             :     COMPOUND_DISTWTD,
     532             :     COMPOUND_DIFFWTD,
     533             :     COMPOUND_WEDGE
     534             : };
     535             : 
     536    72003700 : void determine_compound_mode(
     537             :     PictureControlSet            *picture_control_set_ptr,
     538             :     ModeDecisionContext          *context_ptr,
     539             :     ModeDecisionCandidate        *candidatePtr,
     540             :     MD_COMP_TYPE                 cur_type) {
     541             : 
     542             : 
     543    72003700 :     candidatePtr->interinter_comp.type = to_av1_compound_lut[cur_type];
     544             : 
     545    72003700 :     if (cur_type == MD_COMP_AVG) {
     546             : 
     547    20438900 :         candidatePtr->comp_group_idx = 0;
     548    20438900 :         candidatePtr->compound_idx = 1;
     549             : #if 0
     550             :         if (candidatePtr->merge_flag == 0)
     551             :             search_compound_avg_dist(
     552             :                 picture_control_set_ptr,
     553             :                 context_ptr,
     554             :                 candidatePtr);
     555             : #endif
     556             :     }
     557    51564800 :     else if (cur_type == MD_COMP_DIST) {
     558             : 
     559    20257000 :         candidatePtr->comp_group_idx = 0;
     560    20257000 :         candidatePtr->compound_idx = 0;
     561             :     }
     562    31307800 :     else if (cur_type == MD_COMP_DIFF0) {
     563    19874600 :         candidatePtr->comp_group_idx = 1;
     564    19874600 :         candidatePtr->compound_idx = 1;
     565    19874600 :         candidatePtr->interinter_comp.mask_type = 55;
     566    19874600 :         search_compound_diff_wedge(
     567             :             picture_control_set_ptr,
     568             :             context_ptr,
     569             :             candidatePtr
     570             :         );
     571             : 
     572             :     }
     573             :     //else if (cur_type == MD_COMP_DIFF1) {
     574             :     //    candidatePtr->comp_group_idx = 1;
     575             :     //    candidatePtr->compound_idx = 1;
     576             :     //    candidatePtr->interinter_comp.mask_type = 1;
     577             :     //}
     578    11433100 :     else if (cur_type == MD_COMP_WEDGE) {
     579             : 
     580    11530500 :         candidatePtr->comp_group_idx = 1;
     581    11530500 :         candidatePtr->compound_idx = 1;
     582    11530500 :         search_compound_diff_wedge(
     583             :             picture_control_set_ptr,
     584             :             context_ptr,
     585             :             candidatePtr
     586             :         );
     587             : 
     588    11529300 :         candidatePtr->interinter_comp.wedge_index = candidatePtr->interinter_comp.wedge_index;
     589    11529300 :         candidatePtr->interinter_comp.wedge_sign = candidatePtr->interinter_comp.wedge_sign;
     590             :     }
     591             :     else {
     592           0 :         printf("ERROR: not used comp type\n");
     593             :     }
     594    72097200 : }
     595             : 
     596    34004200 : void ChooseBestAv1MvPred(
     597             :     ModeDecisionContext            *context_ptr,
     598             :     struct MdRateEstimationContext      *md_rate_estimation_ptr,
     599             :     CodingUnit      *cu_ptr,
     600             :     MvReferenceFrame ref_frame,
     601             :     uint8_t              is_compound,
     602             :     PredictionMode    mode,              //NEW or NEW_NEW
     603             :     int16_t             mv0x,
     604             :     int16_t             mv0y,
     605             :     int16_t             mv1x,
     606             :     int16_t             mv1y,
     607             :     uint8_t             *bestDrlIndex,      // output
     608             :     IntMv             bestPredmv[2]      // output
     609             : )
     610             : {
     611             :     uint8_t              drli, maxDrlIndex;
     612             :     IntMv             nearestmv[2];
     613             :     IntMv             nearmv[2];
     614             :     IntMv             ref_mv[2];
     615    34004200 :     uint32_t             bestmvCost = 0xFFFFFFFF;
     616             :     MV                 mv;
     617             : 
     618    34004200 :     maxDrlIndex = GetMaxDrlIndex(cu_ptr->av1xd->ref_mv_count[ref_frame], mode);
     619             :     // maxDrlIndex = 1;
     620             : 
     621   117177000 :     for (drli = 0; drli < maxDrlIndex; drli++) {
     622    83191300 :         get_av1_mv_pred_drl(
     623             :             context_ptr,
     624             :             cu_ptr,
     625             :             ref_frame,
     626             :             is_compound,
     627             :             mode,
     628             :             drli,
     629             :             nearestmv,
     630             :             nearmv,
     631             :             ref_mv);
     632             : 
     633             :         //compute the rate for this drli Cand
     634    83196800 :         mv.row = mv0y;
     635    83196800 :         mv.col = mv0x;
     636             : 
     637   166378000 :         uint32_t mvRate = (uint32_t)eb_av1_mv_bit_cost(
     638             :             &mv,
     639             :             &(ref_mv[0].as_mv),
     640    83196800 :             md_rate_estimation_ptr->nmv_vec_cost,
     641    83196800 :             md_rate_estimation_ptr->nmvcoststack,
     642             :             MV_COST_WEIGHT);
     643             : 
     644    83181100 :         if (is_compound) {
     645    45491100 :             mv.row = mv1y;
     646    45491100 :             mv.col = mv1x;
     647             : 
     648    45486000 :             mvRate += (uint32_t)eb_av1_mv_bit_cost(
     649             :                 &mv,
     650             :                 &(ref_mv[1].as_mv),
     651    45491100 :                 md_rate_estimation_ptr->nmv_vec_cost,
     652    45491100 :                 md_rate_estimation_ptr->nmvcoststack,
     653             :                 MV_COST_WEIGHT);
     654             :         }
     655             : 
     656    83175900 :         if (mvRate < bestmvCost) {
     657    50943300 :             bestmvCost = mvRate;
     658    50943300 :             *bestDrlIndex = drli;
     659    50943300 :             bestPredmv[0] = ref_mv[0];
     660    50943300 :             bestPredmv[1] = ref_mv[1];
     661             :         }
     662             :     }
     663    33985800 : }
     664             : 
     665        2150 : static void mode_decision_candidate_buffer_dctor(EbPtr p)
     666             : {
     667        2150 :     ModeDecisionCandidateBuffer *obj = (ModeDecisionCandidateBuffer*)p;
     668        2150 :     EB_DELETE(obj->prediction_ptr);
     669        2150 :     EB_DELETE(obj->prediction_ptr_temp);
     670        2150 :     EB_DELETE(obj->cfl_temp_prediction_ptr);
     671        2150 :     EB_DELETE(obj->residual_ptr);
     672        2150 :     EB_DELETE(obj->residual_quant_coeff_ptr);
     673        2150 :     EB_DELETE(obj->recon_coeff_ptr);
     674        2150 :     EB_DELETE(obj->recon_ptr);
     675        2150 : }
     676             : #if ENHANCE_ATB
     677          16 : static void mode_decision_scratch_candidate_buffer_dctor(EbPtr p)
     678             : {
     679          16 :     ModeDecisionCandidateBuffer *obj = (ModeDecisionCandidateBuffer*)p;
     680          16 :     EB_DELETE(obj->prediction_ptr);
     681          16 :     EB_DELETE(obj->prediction_ptr_temp);
     682          16 :     EB_DELETE(obj->cfl_temp_prediction_ptr);
     683          16 :     EB_DELETE(obj->residual_ptr);
     684          16 :     EB_DELETE(obj->residual_quant_coeff_ptr);
     685          16 :     EB_DELETE(obj->recon_coeff_ptr);
     686          16 :     EB_DELETE(obj->recon_ptr);
     687          16 : }
     688             : #endif
     689             : /***************************************
     690             : * Mode Decision Candidate Ctor
     691             : ***************************************/
     692        2150 : EbErrorType mode_decision_candidate_buffer_ctor(
     693             :     ModeDecisionCandidateBuffer    *buffer_ptr,
     694             :     EbBitDepthEnum                  max_bitdepth,
     695             :     uint64_t                       *fast_cost_ptr,
     696             :     uint64_t                       *full_cost_ptr,
     697             :     uint64_t                       *full_cost_skip_ptr,
     698             :     uint64_t                       *full_cost_merge_ptr)
     699             : {
     700             :     EbPictureBufferDescInitData pictureBufferDescInitData;
     701             :     EbPictureBufferDescInitData doubleWidthPictureBufferDescInitData;
     702             : 
     703             :     EbPictureBufferDescInitData ThirtyTwoWidthPictureBufferDescInitData;
     704             : 
     705             : 
     706        2150 :     buffer_ptr->dctor = mode_decision_candidate_buffer_dctor;
     707             : 
     708             :     // Init Picture Data
     709        2150 :     pictureBufferDescInitData.max_width = MAX_SB_SIZE;
     710        2150 :     pictureBufferDescInitData.max_height = MAX_SB_SIZE;
     711        2150 :     pictureBufferDescInitData.bit_depth = max_bitdepth;
     712        2150 :     pictureBufferDescInitData.color_format = EB_YUV420;
     713        2150 :     pictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     714        2150 :     pictureBufferDescInitData.left_padding = 0;
     715        2150 :     pictureBufferDescInitData.right_padding = 0;
     716        2150 :     pictureBufferDescInitData.top_padding = 0;
     717        2150 :     pictureBufferDescInitData.bot_padding = 0;
     718        2150 :     pictureBufferDescInitData.split_mode = EB_FALSE;
     719        2150 :     doubleWidthPictureBufferDescInitData.max_width = MAX_SB_SIZE;
     720        2150 :     doubleWidthPictureBufferDescInitData.max_height = MAX_SB_SIZE;
     721        2150 :     doubleWidthPictureBufferDescInitData.bit_depth = EB_16BIT;
     722        2150 :     doubleWidthPictureBufferDescInitData.color_format = EB_YUV420;
     723        2150 :     doubleWidthPictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     724        2150 :     doubleWidthPictureBufferDescInitData.left_padding = 0;
     725        2150 :     doubleWidthPictureBufferDescInitData.right_padding = 0;
     726        2150 :     doubleWidthPictureBufferDescInitData.top_padding = 0;
     727        2150 :     doubleWidthPictureBufferDescInitData.bot_padding = 0;
     728        2150 :     doubleWidthPictureBufferDescInitData.split_mode = EB_FALSE;
     729             : 
     730        2150 :     ThirtyTwoWidthPictureBufferDescInitData.max_width = MAX_SB_SIZE;
     731        2150 :     ThirtyTwoWidthPictureBufferDescInitData.max_height = MAX_SB_SIZE;
     732        2150 :     ThirtyTwoWidthPictureBufferDescInitData.bit_depth = EB_32BIT;
     733        2150 :     ThirtyTwoWidthPictureBufferDescInitData.color_format = EB_YUV420;
     734        2150 :     ThirtyTwoWidthPictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     735        2150 :     ThirtyTwoWidthPictureBufferDescInitData.left_padding = 0;
     736        2150 :     ThirtyTwoWidthPictureBufferDescInitData.right_padding = 0;
     737        2150 :     ThirtyTwoWidthPictureBufferDescInitData.top_padding = 0;
     738        2150 :     ThirtyTwoWidthPictureBufferDescInitData.bot_padding = 0;
     739        2150 :     ThirtyTwoWidthPictureBufferDescInitData.split_mode = EB_FALSE;
     740             : 
     741             :     // Candidate Ptr
     742        2150 :     buffer_ptr->candidate_ptr = (ModeDecisionCandidate*)EB_NULL;
     743             : 
     744             :     // Video Buffers
     745        2150 :     EB_NEW(
     746             :         buffer_ptr->prediction_ptr,
     747             :         eb_picture_buffer_desc_ctor,
     748             :         (EbPtr)&pictureBufferDescInitData);
     749             : 
     750        2150 :     EB_NEW(
     751             :         buffer_ptr->prediction_ptr_temp,
     752             :         eb_picture_buffer_desc_ctor,
     753             :         (EbPtr)&pictureBufferDescInitData);
     754             : 
     755        2150 :     EB_NEW(
     756             :         buffer_ptr->cfl_temp_prediction_ptr,
     757             :         eb_picture_buffer_desc_ctor,
     758             :         (EbPtr)&pictureBufferDescInitData);
     759             : 
     760        2150 :     EB_NEW(
     761             :         buffer_ptr->residual_ptr,
     762             :         eb_picture_buffer_desc_ctor,
     763             :         (EbPtr)&doubleWidthPictureBufferDescInitData);
     764             : 
     765        2150 :     EB_NEW(
     766             :         buffer_ptr->residual_quant_coeff_ptr,
     767             :         eb_picture_buffer_desc_ctor,
     768             :         (EbPtr)&ThirtyTwoWidthPictureBufferDescInitData);
     769             : 
     770        2150 :     EB_NEW(
     771             :         buffer_ptr->recon_coeff_ptr,
     772             :         eb_picture_buffer_desc_ctor,
     773             :         (EbPtr)&ThirtyTwoWidthPictureBufferDescInitData);
     774             : 
     775        2150 :     EB_NEW(
     776             :         buffer_ptr->recon_ptr,
     777             :         eb_picture_buffer_desc_ctor,
     778             :         (EbPtr)&pictureBufferDescInitData);
     779             : 
     780             :     //Distortion
     781        2150 :     buffer_ptr->residual_luma_sad = 0;
     782             : 
     783        2150 :     buffer_ptr->full_lambda_rate = 0;
     784             : 
     785             :     // Costs
     786        2150 :     buffer_ptr->fast_cost_ptr = fast_cost_ptr;
     787        2150 :     buffer_ptr->full_cost_ptr = full_cost_ptr;
     788        2150 :     buffer_ptr->full_cost_skip_ptr = full_cost_skip_ptr;
     789        2150 :     buffer_ptr->full_cost_merge_ptr = full_cost_merge_ptr;
     790        2150 :     return EB_ErrorNone;
     791             : }
     792             : #if ENHANCE_ATB
     793          16 : EbErrorType mode_decision_scratch_candidate_buffer_ctor(
     794             :     ModeDecisionCandidateBuffer    *buffer_ptr,
     795             :     EbBitDepthEnum                  max_bitdepth)
     796             : {
     797             : 
     798             :     EbPictureBufferDescInitData pictureBufferDescInitData;
     799             :     EbPictureBufferDescInitData doubleWidthPictureBufferDescInitData;
     800             :     EbPictureBufferDescInitData ThirtyTwoWidthPictureBufferDescInitData;
     801             : 
     802             : 
     803          16 :     buffer_ptr->dctor = mode_decision_scratch_candidate_buffer_dctor;
     804             : 
     805             :     // Init Picture Data
     806          16 :     pictureBufferDescInitData.max_width = MAX_SB_SIZE;
     807          16 :     pictureBufferDescInitData.max_height = MAX_SB_SIZE;
     808          16 :     pictureBufferDescInitData.bit_depth = max_bitdepth;
     809          16 :     pictureBufferDescInitData.color_format = EB_YUV420;
     810          16 :     pictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     811          16 :     pictureBufferDescInitData.left_padding = 0;
     812          16 :     pictureBufferDescInitData.right_padding = 0;
     813          16 :     pictureBufferDescInitData.top_padding = 0;
     814          16 :     pictureBufferDescInitData.bot_padding = 0;
     815          16 :     pictureBufferDescInitData.split_mode = EB_FALSE;
     816          16 :     doubleWidthPictureBufferDescInitData.max_width = MAX_SB_SIZE;
     817          16 :     doubleWidthPictureBufferDescInitData.max_height = MAX_SB_SIZE;
     818          16 :     doubleWidthPictureBufferDescInitData.bit_depth = EB_16BIT;
     819          16 :     doubleWidthPictureBufferDescInitData.color_format = EB_YUV420;
     820          16 :     doubleWidthPictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     821          16 :     doubleWidthPictureBufferDescInitData.left_padding = 0;
     822          16 :     doubleWidthPictureBufferDescInitData.right_padding = 0;
     823          16 :     doubleWidthPictureBufferDescInitData.top_padding = 0;
     824          16 :     doubleWidthPictureBufferDescInitData.bot_padding = 0;
     825          16 :     doubleWidthPictureBufferDescInitData.split_mode = EB_FALSE;
     826             : 
     827          16 :     ThirtyTwoWidthPictureBufferDescInitData.max_width = MAX_SB_SIZE;
     828          16 :     ThirtyTwoWidthPictureBufferDescInitData.max_height = MAX_SB_SIZE;
     829          16 :     ThirtyTwoWidthPictureBufferDescInitData.bit_depth = EB_32BIT;
     830          16 :     ThirtyTwoWidthPictureBufferDescInitData.color_format = EB_YUV420;
     831          16 :     ThirtyTwoWidthPictureBufferDescInitData.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
     832          16 :     ThirtyTwoWidthPictureBufferDescInitData.left_padding = 0;
     833          16 :     ThirtyTwoWidthPictureBufferDescInitData.right_padding = 0;
     834          16 :     ThirtyTwoWidthPictureBufferDescInitData.top_padding = 0;
     835          16 :     ThirtyTwoWidthPictureBufferDescInitData.bot_padding = 0;
     836          16 :     ThirtyTwoWidthPictureBufferDescInitData.split_mode = EB_FALSE;
     837             : 
     838             :     // Candidate Ptr
     839          16 :     buffer_ptr->candidate_ptr = (ModeDecisionCandidate*)EB_NULL;
     840             : 
     841             :     // Video Buffers
     842          16 :     EB_NEW(
     843             :         buffer_ptr->prediction_ptr,
     844             :         eb_picture_buffer_desc_ctor,
     845             :         (EbPtr)&pictureBufferDescInitData);
     846             : 
     847          16 :     EB_NEW(
     848             :         buffer_ptr->prediction_ptr_temp,
     849             :         eb_picture_buffer_desc_ctor,
     850             :         (EbPtr)&pictureBufferDescInitData);
     851             : 
     852          16 :     EB_NEW(
     853             :         buffer_ptr->cfl_temp_prediction_ptr,
     854             :         eb_picture_buffer_desc_ctor,
     855             :         (EbPtr)&pictureBufferDescInitData);
     856             : 
     857          16 :     EB_NEW(
     858             :         buffer_ptr->residual_ptr,
     859             :         eb_picture_buffer_desc_ctor,
     860             :         (EbPtr)&doubleWidthPictureBufferDescInitData);
     861             : 
     862          16 :     EB_NEW(
     863             :         buffer_ptr->residual_quant_coeff_ptr,
     864             :         eb_picture_buffer_desc_ctor,
     865             :         (EbPtr)&ThirtyTwoWidthPictureBufferDescInitData);
     866             : 
     867          16 :     EB_NEW(
     868             :         buffer_ptr->recon_coeff_ptr,
     869             :         eb_picture_buffer_desc_ctor,
     870             :         (EbPtr)&ThirtyTwoWidthPictureBufferDescInitData);
     871             : 
     872          16 :     EB_NEW(
     873             :         buffer_ptr->recon_ptr,
     874             :         eb_picture_buffer_desc_ctor,
     875             :         (EbPtr)&pictureBufferDescInitData);
     876          16 :     return EB_ErrorNone;
     877             : }
     878             : #endif
     879             : 
     880     6422250 : uint8_t check_ref_beackout(
     881             :     struct ModeDecisionContext *context_ptr,
     882             :     uint8_t                     ref_frame_type,
     883             :     PART                        shape)
     884             : {
     885     6422250 :     uint8_t skip_candidate = 0;
     886     6422250 :     uint8_t ref_cnt = 0;
     887     6422250 :     uint8_t allowed_nsq_ref_th = (uint8_t)PRUNE_REC_TH;
     888     6422250 :     if (context_ptr->prune_ref_frame_for_rec_partitions) {
     889      201896 :         if (shape != PART_N) {
     890             :             uint8_t ref_idx;
     891           0 :             assert(ref_frame_type < 30);
     892           0 :             ref_cnt = 0;
     893           0 :             for (ref_idx = 0; ref_idx < allowed_nsq_ref_th; ref_idx++) {
     894           0 :                 if (ref_frame_type == context_ptr->ref_best_ref_sq_table[ref_idx]) {
     895           0 :                     ref_cnt++;
     896             :                 }
     897             :             }
     898           0 :             skip_candidate = ref_cnt ? 0 : 1;
     899             :         }
     900             :     }
     901     6422250 :     return skip_candidate;
     902             : }
     903             : /***************************************
     904             : * return true if the MV candidate is already injected
     905             : ***************************************/
     906     7553920 : EbBool mrp_is_already_injected_mv_l0(
     907             :     ModeDecisionContext *context_ptr,
     908             :     int16_t                mv_x,
     909             :     int16_t                mv_y,
     910             :     uint8_t                ref_type) {
     911   134268000 :     for (int inter_candidate_index = 0; inter_candidate_index < context_ptr->injected_mv_count_l0; inter_candidate_index++) {
     912   126946000 :         if (context_ptr->injected_mv_x_l0_array[inter_candidate_index] == mv_x &&
     913    10363500 :             context_ptr->injected_mv_y_l0_array[inter_candidate_index] == mv_y &&
     914     4068230 :             context_ptr->injected_ref_type_l0_array[inter_candidate_index] == ref_type) {
     915      231579 :             return(EB_TRUE);
     916             :         }
     917             :     }
     918             : 
     919     7322340 :     return(EB_FALSE);
     920             : }
     921             : 
     922     5125800 : EbBool mrp_is_already_injected_mv_l1(
     923             :     ModeDecisionContext *context_ptr,
     924             :     int16_t                mv_x,
     925             :     int16_t                mv_y,
     926             :     uint8_t                ref_type) {
     927    65244400 :     for (int inter_candidate_index = 0; inter_candidate_index < context_ptr->injected_mv_count_l1; inter_candidate_index++) {
     928    60307300 :         if (context_ptr->injected_mv_x_l1_array[inter_candidate_index] == mv_x &&
     929     5447550 :             context_ptr->injected_mv_y_l1_array[inter_candidate_index] == mv_y &&
     930     1138640 :             context_ptr->injected_ref_type_l1_array[inter_candidate_index] == ref_type) {
     931      188730 :             return(EB_TRUE);
     932             :         }
     933             :     }
     934             : 
     935     4937070 :     return(EB_FALSE);
     936             : }
     937             : 
     938    20009600 : EbBool mrp_is_already_injected_mv_bipred(
     939             :     ModeDecisionContext *context_ptr,
     940             :     int16_t                mv_x_l0,
     941             :     int16_t                mv_y_l0,
     942             :     int16_t                mv_x_l1,
     943             :     int16_t                mv_y_l1,
     944             :     uint8_t                ref_type) {
     945  1875260000 :     for (int inter_candidate_index = 0; inter_candidate_index < context_ptr->injected_mv_count_bipred; inter_candidate_index++) {
     946  1855710000 :         if (context_ptr->injected_mv_x_bipred_l0_array[inter_candidate_index] == mv_x_l0 &&
     947   311304000 :             context_ptr->injected_mv_y_bipred_l0_array[inter_candidate_index] == mv_y_l0 &&
     948   213139000 :             context_ptr->injected_mv_x_bipred_l1_array[inter_candidate_index] == mv_x_l1 &&
     949    24256000 :             context_ptr->injected_mv_y_bipred_l1_array[inter_candidate_index] == mv_y_l1 &&
     950    12152400 :             context_ptr->injected_ref_type_bipred_array[inter_candidate_index] == ref_type) {
     951      455461 :             return(EB_TRUE);
     952             :         }
     953             :     }
     954    19554100 :     return(EB_FALSE);
     955             : }
     956             : 
     957             : #define BIPRED_3x3_REFINMENT_POSITIONS 8
     958             : 
     959             : int8_t ALLOW_REFINEMENT_FLAG[BIPRED_3x3_REFINMENT_POSITIONS] = {  1, 0, 1, 0, 1,  0,  1, 0 };
     960             : int8_t BIPRED_3x3_X_POS[BIPRED_3x3_REFINMENT_POSITIONS] = { -1, -1, 0, 1, 1, 1, 0, -1 };
     961             : int8_t BIPRED_3x3_Y_POS[BIPRED_3x3_REFINMENT_POSITIONS] = { 0, 1, 1, 1, 0, -1, -1, -1 };
     962             : 
     963      145717 : void Unipred3x3CandidatesInjection(
     964             :     const SequenceControlSet  *sequence_control_set_ptr,
     965             :     PictureControlSet         *picture_control_set_ptr,
     966             :     ModeDecisionContext       *context_ptr,
     967             :     LargestCodingUnit         *sb_ptr,
     968             :     uint32_t                   me_sb_addr,
     969             :     SsMeContext               *inloop_me_context,
     970             :     EbBool                     use_close_loop_me,
     971             :     uint32_t                   close_loop_me_index,
     972             :     uint32_t                  *candidateTotalCnt){
     973             :     UNUSED(sb_ptr);
     974             :     uint32_t                   bipredIndex;
     975      145717 :     uint32_t                   canTotalCnt = (*candidateTotalCnt);
     976      145717 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
     977      145717 :     const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[me_sb_addr];
     978      145717 :     uint8_t total_me_cnt = me_results->total_me_candidate_index[context_ptr->me_block_offset];
     979      145717 :     const MeCandidate *me_block_results = me_results->me_candidate[context_ptr->me_block_offset];
     980      145717 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
     981      145717 :     EbBool isCompoundEnabled = (frm_hdr->reference_mode == SINGLE_REFERENCE) ? 0 : 1;
     982      145717 :     IntMv  bestPredmv[2] = { {0}, {0} };
     983      145717 :     int inside_tile = 1;
     984      145717 :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
     985      145717 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
     986      145717 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
     987      145717 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
     988             : 
     989             :     // (8 Best_L0 neighbors)
     990             :     //const MeLcuResults_t *meResults = pictureControlSetPtr->ParentPcsPtr->meResultsPtr[lcuAddr];
     991      145717 :     total_me_cnt = MIN(total_me_cnt, BEST_CANDIDATE_COUNT);
     992      665850 :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index)
     993             :     {
     994      520138 :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
     995      520138 :         const uint8_t inter_direction = me_block_results_ptr->direction;
     996      520138 :         const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
     997             : 
     998      520138 :         if (inter_direction == 0) {
     999     1097890 :     for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex)
    1000             :     {
    1001             :         /**************
    1002             :         NEWMV L0
    1003             :         ************* */
    1004      975906 :         if (context_ptr->unipred3x3_injection >= 2){
    1005           0 :             if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1006           0 :                 continue;
    1007             :         }
    1008      975906 :         int16_t to_inject_mv_x = use_close_loop_me ? (inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] + BIPRED_3x3_X_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv + BIPRED_3x3_X_POS[bipredIndex]) << 1;
    1009      975906 :         int16_t to_inject_mv_y = use_close_loop_me ? (inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] + BIPRED_3x3_Y_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv + BIPRED_3x3_Y_POS[bipredIndex]) << 1;
    1010      975906 :         uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    1011      975905 :         uint8_t skip_cand = check_ref_beackout(
    1012             :             context_ptr,
    1013             :             to_inject_ref_type,
    1014      975905 :             context_ptr->blk_geom->shape);
    1015             : 
    1016      975902 :         inside_tile = 1;
    1017      975902 :         if(umv0tile)
    1018           0 :             inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1019      975902 :         skip_cand = skip_cand || (!inside_tile);
    1020      975902 :         if (!skip_cand && (context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE)) {
    1021             : 
    1022             : #if II_COMP_FLAG // 3x3  L0
    1023             :              //MvReferenceFrame rf[2];
    1024             :              //rf[0] = to_inject_ref_type;
    1025             :              //rf[1] = -1;
    1026             : 
    1027             :              uint8_t inter_type;
    1028      947323 :              uint8_t is_ii_allowed = 0;//svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    1029      947323 :              uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    1030             :              //uint8_t is_obmc_allowed =  obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    1031             :              //tot_inter_types = is_obmc_allowed ? tot_inter_types+1 : tot_inter_types;
    1032             : 
    1033     1894640 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    1034             :             {
    1035             : #endif
    1036      947323 :             candidateArray[canTotalCnt].type = INTER_MODE;
    1037      947323 :             candidateArray[canTotalCnt].distortion_ready = 0;
    1038      947323 :             candidateArray[canTotalCnt].use_intrabc = 0;
    1039      947323 :             candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1040      947323 :             candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    1041      947323 :             candidateArray[canTotalCnt].inter_mode = NEWMV;
    1042      947323 :             candidateArray[canTotalCnt].pred_mode = NEWMV;
    1043      947323 :             candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1044             : 
    1045      947323 :             candidateArray[canTotalCnt].is_compound = 0;
    1046      947323 :             candidateArray[canTotalCnt].is_new_mv = 1;
    1047      947323 :             candidateArray[canTotalCnt].is_zero_mv = 0;
    1048             : 
    1049      947323 :             candidateArray[canTotalCnt].drl_index = 0;
    1050             : 
    1051             :             // Set the MV to ME result
    1052      947323 :             candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x;
    1053      947323 :             candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y;
    1054             : 
    1055             :             // will be needed later by the rate estimation
    1056      947323 :             candidateArray[canTotalCnt].ref_mv_index = 0;
    1057      947323 :             candidateArray[canTotalCnt].pred_mv_weight = 0;
    1058      947323 :             candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    1059      947327 :             candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1060      947327 :             candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    1061             : 
    1062      947327 :             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    1063      947327 :             candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    1064             : 
    1065      947327 :             ChooseBestAv1MvPred(
    1066             :                 context_ptr,
    1067      947327 :                 candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1068             :                 context_ptr->cu_ptr,
    1069      947327 :                 candidateArray[canTotalCnt].ref_frame_type,
    1070      947327 :                 candidateArray[canTotalCnt].is_compound,
    1071      947327 :                 candidateArray[canTotalCnt].pred_mode,
    1072      947327 :                 candidateArray[canTotalCnt].motion_vector_xl0,
    1073      947327 :                 candidateArray[canTotalCnt].motion_vector_yl0,
    1074             :                 0, 0,
    1075      947327 :                 &candidateArray[canTotalCnt].drl_index,
    1076             :                 bestPredmv);
    1077             : 
    1078      947313 :             candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1079      947313 :             candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1080             : #if II_COMP_FLAG
    1081      947313 :             if (inter_type == 0) {
    1082      947313 :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    1083      947313 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1084             :             }
    1085             :             else {
    1086           0 :                 if (is_ii_allowed) {
    1087           0 :                     if (inter_type == 1) {
    1088           0 :                         inter_intra_search(
    1089             :                             picture_control_set_ptr,
    1090             :                             context_ptr,
    1091           0 :                             &candidateArray[canTotalCnt]);
    1092           0 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    1093           0 :                         candidateArray[canTotalCnt].use_wedge_interintra = 1;
    1094           0 :                         candidateArray[canTotalCnt].ii_wedge_sign = 0;
    1095             :                     }
    1096           0 :                     else if (inter_type == 2) {
    1097           0 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    1098           0 :                         candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    1099           0 :                         candidateArray[canTotalCnt].use_wedge_interintra = 0;
    1100             :                     }
    1101             :                 }
    1102             :                 //if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    1103             :                 //    candidateArray[canTotalCnt].is_interintra_used = 0;
    1104             :                 //    candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    1105             :                 //}
    1106             :             }
    1107             : #endif
    1108      947313 :             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1109             : 
    1110             : #if II_COMP_FLAG
    1111             :             }
    1112             : #endif
    1113      947313 :             context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    1114      947313 :             context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    1115      947313 :             context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    1116      947313 :             ++context_ptr->injected_mv_count_l0;
    1117             :         }
    1118             :            }
    1119             :         }
    1120             :     }
    1121             : 
    1122             :     // (8 Best_L1 neighbors)
    1123             : //const MeLcuResults_t *meResults = pictureControlSetPtr->ParentPcsPtr->meResultsPtr[lcuAddr];
    1124      145712 :     total_me_cnt = MIN(total_me_cnt, BEST_CANDIDATE_COUNT);
    1125      665844 :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index)
    1126             :     {
    1127      520126 :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    1128      520126 :         const uint8_t inter_direction = me_block_results_ptr->direction;
    1129      520126 :         const uint8_t list1_ref_index = me_block_results_ptr->ref_idx_l1;
    1130      520126 :         if (inter_direction == 1) {
    1131     1131490 :     for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex)
    1132             :     {
    1133     1005760 :         if (isCompoundEnabled) {
    1134             :             /**************
    1135             :             NEWMV L1
    1136             :             ************* */
    1137     1005760 :             if (context_ptr->unipred3x3_injection >= 2) {
    1138           0 :                 if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1139           0 :                     continue;
    1140             :             }
    1141     1005760 :             int16_t to_inject_mv_x = use_close_loop_me ? (inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] + BIPRED_3x3_X_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].x_mv + BIPRED_3x3_X_POS[bipredIndex]) << 1;
    1142     1005760 :             int16_t to_inject_mv_y = use_close_loop_me ? (inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] + BIPRED_3x3_Y_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].y_mv + BIPRED_3x3_Y_POS[bipredIndex]) << 1;
    1143     1005760 :             uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    1144     1005760 :             uint8_t skip_cand = check_ref_beackout(
    1145             :                 context_ptr,
    1146             :                 to_inject_ref_type,
    1147     1005760 :                 context_ptr->blk_geom->shape);
    1148             : 
    1149     1005760 :             inside_tile = 1;
    1150     1005760 :             if(umv0tile)
    1151           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1152     1005760 :             skip_cand = skip_cand || (!inside_tile);
    1153     1005760 :             if (!skip_cand && (context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE)) {
    1154             : #if II_COMP_FLAG // 3x3  L1
    1155             :              //MvReferenceFrame rf[2];
    1156             :              //rf[0] = to_inject_ref_type;
    1157             :              //rf[1] = -1;
    1158             :              uint8_t inter_type;
    1159      977809 :              uint8_t is_ii_allowed = 0;//svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    1160      977809 :              uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    1161             :              //uint8_t is_obmc_allowed =  obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    1162             :              //tot_inter_types = is_obmc_allowed ? tot_inter_types+1 : tot_inter_types;
    1163     1955620 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    1164             :             {
    1165             : #endif
    1166      977807 :                 candidateArray[canTotalCnt].type = INTER_MODE;
    1167      977807 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    1168      977807 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    1169      977807 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1170      977807 :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)1;
    1171      977807 :                 candidateArray[canTotalCnt].inter_mode = NEWMV;
    1172      977807 :                 candidateArray[canTotalCnt].pred_mode = NEWMV;
    1173      977807 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1174             : 
    1175      977807 :                 candidateArray[canTotalCnt].is_compound = 0;
    1176      977807 :                 candidateArray[canTotalCnt].is_new_mv = 1;
    1177      977807 :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    1178             : 
    1179      977807 :                 candidateArray[canTotalCnt].drl_index = 0;
    1180             : 
    1181             :                 // Set the MV to ME result
    1182      977807 :                 candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x;
    1183      977807 :                 candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y;
    1184             :                 // will be needed later by the rate estimation
    1185      977807 :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    1186      977807 :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    1187      977807 :                 candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    1188      977811 :                 candidateArray[canTotalCnt].ref_frame_index_l0 = -1;
    1189      977811 :                 candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1190      977811 :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    1191      977811 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    1192      977811 :                 ChooseBestAv1MvPred(
    1193             :                     context_ptr,
    1194      977811 :                     candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1195             :                     context_ptr->cu_ptr,
    1196      977811 :                     candidateArray[canTotalCnt].ref_frame_type,
    1197      977811 :                     candidateArray[canTotalCnt].is_compound,
    1198      977811 :                     candidateArray[canTotalCnt].pred_mode,
    1199      977811 :                     candidateArray[canTotalCnt].motion_vector_xl1,
    1200      977811 :                     candidateArray[canTotalCnt].motion_vector_yl1,
    1201             :                     0, 0,
    1202      977811 :                     &candidateArray[canTotalCnt].drl_index,
    1203             :                     bestPredmv);
    1204             : 
    1205      977812 :                 candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[0].as_mv.col;
    1206      977812 :                 candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[0].as_mv.row;
    1207             : #if II_COMP_FLAG
    1208      977812 :             if (inter_type == 0) {
    1209      977812 :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    1210      977812 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1211             :             }
    1212             :             else {
    1213           0 :                 if (is_ii_allowed) {
    1214           0 :                     if (inter_type == 1) {
    1215           0 :                         inter_intra_search(
    1216             :                             picture_control_set_ptr,
    1217             :                             context_ptr,
    1218           0 :                             &candidateArray[canTotalCnt]);
    1219           0 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    1220           0 :                         candidateArray[canTotalCnt].use_wedge_interintra = 1;
    1221           0 :                         candidateArray[canTotalCnt].ii_wedge_sign = 0;
    1222             :                     }
    1223           0 :                     else if (inter_type == 2) {
    1224           0 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    1225           0 :                         candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    1226           0 :                         candidateArray[canTotalCnt].use_wedge_interintra = 0;
    1227             :                     }
    1228             :                 }
    1229             :                 //if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    1230             :                 //    candidateArray[canTotalCnt].is_interintra_used = 0;
    1231             :                 //    candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    1232             :                 //}
    1233             :             }
    1234             : #endif
    1235      977812 :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1236             : #if II_COMP_FLAG
    1237             :             }
    1238             : #endif
    1239      977814 :                 context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    1240      977814 :                 context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    1241      977814 :                 context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = to_inject_ref_type;
    1242      977814 :                 ++context_ptr->injected_mv_count_l1;
    1243             :             }
    1244             :         }
    1245             :     }
    1246             :         }
    1247             :     }
    1248             : 
    1249             :     // update the total number of candidates injected
    1250      145718 :     (*candidateTotalCnt) = canTotalCnt;
    1251             : 
    1252      145718 :     return;
    1253             : }
    1254             : 
    1255       92804 : void Bipred3x3CandidatesInjection(
    1256             :     const SequenceControlSet *sequence_control_set_ptr,
    1257             :     PictureControlSet        *picture_control_set_ptr,
    1258             :     ModeDecisionContext      *context_ptr,
    1259             :     LargestCodingUnit        *sb_ptr,
    1260             :     uint32_t                  me_sb_addr,
    1261             :     SsMeContext              *inloop_me_context,
    1262             :     EbBool                    use_close_loop_me,
    1263             :     uint32_t                  close_loop_me_index,
    1264             :     uint32_t                 *candidateTotalCnt){
    1265             :     UNUSED(sb_ptr);
    1266             :     uint32_t                   bipredIndex;
    1267       92804 :     uint32_t                   canTotalCnt = (*candidateTotalCnt);
    1268       92804 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    1269       92804 :     const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[me_sb_addr];
    1270       92804 :     uint8_t total_me_cnt = me_results->total_me_candidate_index[context_ptr->me_block_offset];
    1271       92804 :     const MeCandidate *me_block_results = me_results->me_candidate[context_ptr->me_block_offset];
    1272       92804 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    1273       92804 :     EbBool isCompoundEnabled = (frm_hdr->reference_mode == SINGLE_REFERENCE) ? 0 : 1;
    1274       92804 :     IntMv  bestPredmv[2] = { {0}, {0} };
    1275       92804 :     int inside_tile = 1;
    1276       92804 :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    1277       92804 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    1278       92804 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    1279       92804 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    1280             :     MD_COMP_TYPE cur_type; //BIP 3x3
    1281             : #if FIX_COMPOUND
    1282       92804 :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    1283       92804 :     MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    1284      185608 :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    1285       92804 :         (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    1286             :         (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    1287           0 :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    1288             : #else
    1289             :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    1290             :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    1291             : #endif
    1292             : #if SPEED_OPT
    1293       92804 :     if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    1294       18151 :         tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    1295             : #endif
    1296       92804 :     if (isCompoundEnabled) {
    1297             :         /**************
    1298             :        NEW_NEWMV
    1299             :        ************* */
    1300             :        //const MeLcuResults_t *meResults = pictureControlSetPtr->ParentPcsPtr->meResultsPtr[lcuAddr];
    1301       92805 :         total_me_cnt = MIN(total_me_cnt, BEST_CANDIDATE_COUNT);
    1302      430920 :         for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index)
    1303             :         {
    1304      338228 :             const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    1305      338228 :             const uint8_t inter_direction = me_block_results_ptr->direction;
    1306      338228 :             const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
    1307      338228 :             const uint8_t list1_ref_index = me_block_results_ptr->ref_idx_l1;
    1308             : 
    1309      338228 :             if (inter_direction == 2) {
    1310             :        // (Best_L0, 8 Best_L1 neighbors)
    1311     1678160 :         for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex)
    1312             :         {
    1313     1491750 :         if (context_ptr->bipred3x3_injection >= 2){
    1314           0 :             if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1315           0 :                 continue;
    1316             :         }
    1317     1491750 :         int16_t to_inject_mv_x_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv << 1;
    1318     1491750 :         int16_t to_inject_mv_y_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv << 1;
    1319     1491750 :         int16_t to_inject_mv_x_l1 = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] + BIPRED_3x3_X_POS[bipredIndex]) << 1) : (me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].x_mv + BIPRED_3x3_X_POS[bipredIndex]) << 1;
    1320     1491750 :         int16_t to_inject_mv_y_l1 = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] + BIPRED_3x3_Y_POS[bipredIndex]) << 1) : (me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].y_mv + BIPRED_3x3_Y_POS[bipredIndex]) << 1;
    1321             : 
    1322             :         MvReferenceFrame rf[2];
    1323     1491750 :         rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1324     1491740 :         rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1325     1491730 :         uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    1326     1491740 :         uint8_t skip_cand = check_ref_beackout(
    1327             :             context_ptr,
    1328             :             to_inject_ref_type,
    1329     1491740 :             context_ptr->blk_geom->shape);
    1330             : 
    1331     1491740 :         inside_tile = 1;
    1332     1491740 :         if(umv0tile) {
    1333           0 :             inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    1334           0 :                           is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1335             :         }
    1336     1491740 :         skip_cand = skip_cand || (!inside_tile);
    1337     1491740 :         if (!skip_cand && (context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE)) {
    1338     1480930 :             context_ptr->variance_ready = 0;
    1339     6974770 :             for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    1340             :             {
    1341     5493920 :                 if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    1342             :                 // If two predictors are very similar, skip wedge compound mode search
    1343     5493920 :                 if (context_ptr->variance_ready)
    1344           0 :                     if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEWMV) && context_ptr->prediction_mse < 64))
    1345           0 :                         continue;
    1346             : 
    1347     5493920 :             candidateArray[canTotalCnt].type = INTER_MODE;
    1348     5493920 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    1349     5493920 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    1350     5493920 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1351     5493920 :                 candidateArray[canTotalCnt].is_new_mv = 1;
    1352     5493920 :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    1353             : 
    1354     5493920 :             candidateArray[canTotalCnt].drl_index = 0;
    1355             : 
    1356             :             // Set the MV to ME result
    1357     5493920 :             candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    1358     5493920 :             candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    1359     5493920 :             candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    1360     5493920 :             candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    1361             :             // will be needed later by the rate estimation
    1362     5493920 :             candidateArray[canTotalCnt].ref_mv_index = 0;
    1363     5493920 :             candidateArray[canTotalCnt].pred_mv_weight = 0;
    1364             : 
    1365     5493920 :             candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    1366     5493920 :             candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    1367     5493920 :             candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1368     5493920 :             candidateArray[canTotalCnt].is_compound = 1;
    1369             : #if II_COMP_FLAG
    1370     5493920 :             candidateArray[canTotalCnt].is_interintra_used = 0;
    1371             : #endif
    1372     5493920 :             candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    1373             :             MvReferenceFrame rf[2];
    1374     5493920 :             rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1375     5494030 :             rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1376     5494000 :             candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    1377     5493910 :             candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1378     5493910 :             candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1379     5493910 :             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    1380     5493910 :             candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    1381     5493910 :             ChooseBestAv1MvPred(
    1382             :                 context_ptr,
    1383     5493910 :                 candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1384             :                 context_ptr->cu_ptr,
    1385     5493910 :                 candidateArray[canTotalCnt].ref_frame_type,
    1386     5493910 :                 candidateArray[canTotalCnt].is_compound,
    1387     5493910 :                 candidateArray[canTotalCnt].pred_mode,
    1388     5493910 :                 candidateArray[canTotalCnt].motion_vector_xl0,
    1389     5493910 :                 candidateArray[canTotalCnt].motion_vector_yl0,
    1390     5493910 :                 candidateArray[canTotalCnt].motion_vector_xl1,
    1391     5493910 :                 candidateArray[canTotalCnt].motion_vector_yl1,
    1392     5493910 :                 &candidateArray[canTotalCnt].drl_index,
    1393             :                 bestPredmv);
    1394             : 
    1395     5493850 :             candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1396     5493850 :             candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1397     5493850 :             candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    1398     5493850 :             candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    1399             :             //BIP 3x3
    1400     5493850 :             determine_compound_mode(
    1401             :                 picture_control_set_ptr,
    1402             :                 context_ptr,
    1403     5493850 :                 &candidateArray[canTotalCnt],
    1404             :                 cur_type);
    1405     5493900 :             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1406     5493840 :             context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    1407     5493840 :             context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    1408     5493840 :             context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    1409     5493840 :             context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    1410     5493840 :             context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    1411     5493840 :             ++context_ptr->injected_mv_count_bipred;
    1412             :             }
    1413             :         }
    1414             :         }
    1415             : 
    1416             :         // (8 Best_L0 neighbors, Best_L1) :
    1417     1678130 :         for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex)
    1418             :         {
    1419     1491770 :             if (context_ptr->bipred3x3_injection >= 2){
    1420           0 :                 if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1421           0 :                     continue;
    1422             :             }
    1423     1491770 :             int16_t to_inject_mv_x_l0 = use_close_loop_me ? (inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] + BIPRED_3x3_X_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv + BIPRED_3x3_X_POS[bipredIndex]) << 1;
    1424     1491770 :             int16_t to_inject_mv_y_l0 = use_close_loop_me ? (inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] + BIPRED_3x3_Y_POS[bipredIndex]) << 1 : (me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv + BIPRED_3x3_Y_POS[bipredIndex]) << 1;
    1425     1491770 :             int16_t to_inject_mv_x_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].x_mv << 1;
    1426     1491770 :             int16_t to_inject_mv_y_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].y_mv << 1;
    1427             : 
    1428             :             MvReferenceFrame rf[2];
    1429     1491770 :             rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1430     1491770 :             rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1431     1491770 :             uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    1432     1491760 :             uint8_t skip_cand = check_ref_beackout(
    1433             :                 context_ptr,
    1434             :                 to_inject_ref_type,
    1435     1491760 :                 context_ptr->blk_geom->shape);
    1436             : 
    1437     1491760 :             inside_tile = 1;
    1438     1491760 :             if(umv0tile) {
    1439           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    1440           0 :                               is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1441             :             }
    1442     1491760 :             skip_cand = skip_cand || (!inside_tile);
    1443     1491760 :             if (!skip_cand && (context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE)) {
    1444     1480350 :                 context_ptr->variance_ready = 0;
    1445     6972280 :                 for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    1446             :                 {
    1447     5491980 :                     if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    1448             :                     // If two predictors are very similar, skip wedge compound mode search
    1449     5491980 :                     if (context_ptr->variance_ready)
    1450           0 :                         if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEWMV) && context_ptr->prediction_mse < 64))
    1451           0 :                             continue;
    1452     5491980 :                 candidateArray[canTotalCnt].type = INTER_MODE;
    1453     5491980 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    1454     5491980 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    1455     5491980 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1456             : 
    1457     5491980 :                 candidateArray[canTotalCnt].is_new_mv = 1;
    1458     5491980 :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    1459             : 
    1460     5491980 :                 candidateArray[canTotalCnt].drl_index = 0;
    1461             : 
    1462             :                 // Set the MV to ME result
    1463     5491980 :                 candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    1464     5491980 :                 candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    1465     5491980 :                 candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    1466     5491980 :                 candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    1467             :                 // will be needed later by the rate estimation
    1468     5491980 :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    1469     5491980 :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    1470             : 
    1471     5491980 :                 candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    1472     5491980 :                 candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    1473     5491980 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1474     5491980 :                 candidateArray[canTotalCnt].is_compound = 1;
    1475             : #if II_COMP_FLAG
    1476     5491980 :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    1477             : #endif
    1478     5491980 :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    1479             :                 MvReferenceFrame rf[2];
    1480     5491980 :                 rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1481     5492040 :                 rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1482     5492010 :                 candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    1483     5491870 :                 candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1484     5491870 :                 candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1485     5491870 :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    1486     5491870 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    1487     5491870 :                 ChooseBestAv1MvPred(
    1488             :                     context_ptr,
    1489     5491870 :                     candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1490             :                     context_ptr->cu_ptr,
    1491     5491870 :                     candidateArray[canTotalCnt].ref_frame_type,
    1492     5491870 :                     candidateArray[canTotalCnt].is_compound,
    1493     5491870 :                     candidateArray[canTotalCnt].pred_mode,
    1494     5491870 :                     candidateArray[canTotalCnt].motion_vector_xl0,
    1495     5491870 :                     candidateArray[canTotalCnt].motion_vector_yl0,
    1496     5491870 :                     candidateArray[canTotalCnt].motion_vector_xl1,
    1497     5491870 :                     candidateArray[canTotalCnt].motion_vector_yl1,
    1498     5491870 :                     &candidateArray[canTotalCnt].drl_index,
    1499             :                     bestPredmv);
    1500             : 
    1501     5491850 :                 candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1502     5491850 :                 candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1503     5491850 :                 candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    1504     5491850 :                 candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    1505             :                 //BIP 3x3
    1506     5491850 :                 determine_compound_mode(
    1507             :                     picture_control_set_ptr,
    1508             :                     context_ptr,
    1509     5491850 :                     &candidateArray[canTotalCnt],
    1510             :                     cur_type);
    1511     5491940 :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1512     5491940 :                 context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    1513     5491940 :                 context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    1514     5491940 :                 context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    1515     5491940 :                 context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    1516     5491940 :                 context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    1517     5491940 :                 ++context_ptr->injected_mv_count_bipred;
    1518             :                 }
    1519             :             }
    1520             :         }
    1521             :             }
    1522             :         }
    1523             :      }
    1524             : 
    1525             :     // update the total number of candidates injected
    1526       92691 :     (*candidateTotalCnt) = canTotalCnt;
    1527             : 
    1528       92691 :     return;
    1529             : }
    1530             : #if EIGTH_PEL_MV && !EIGHT_PEL_PREDICTIVE_ME
    1531             : void eighth_pel_unipred_refinement(
    1532             :     const SequenceControlSet  *sequence_control_set_ptr,
    1533             :     PictureControlSet         *picture_control_set_ptr,
    1534             :     ModeDecisionContext       *context_ptr,
    1535             :     uint32_t                   me_sb_addr,
    1536             :     SsMeContext               *inloop_me_context,
    1537             :     EbBool                     use_close_loop_me,
    1538             :     uint32_t                   close_loop_me_index,
    1539             :     uint32_t                  *candidateTotalCnt) {
    1540             :     uint32_t                   bipredIndex;
    1541             :     uint32_t                   canTotalCnt = (*candidateTotalCnt);
    1542             :     const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[me_sb_addr];
    1543             :     uint8_t total_me_cnt = me_results->total_me_candidate_index[context_ptr->me_block_offset];
    1544             :     const MeCandidate *me_block_results = me_results->me_candidate[context_ptr->me_block_offset];
    1545             :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    1546             :     EbBool isCompoundEnabled = (picture_control_set_ptr->parent_pcs_ptr->reference_mode == SINGLE_REFERENCE) ? 0 : 1;
    1547             :     IntMv  bestPredmv[2] = { {0}, {0} };
    1548             :     uint8_t inj_mv;
    1549             :     int inside_tile = 1;
    1550             :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    1551             :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    1552             :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    1553             :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    1554             : 
    1555             :     // (8 Best_L0 neighbors)
    1556             :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index) {
    1557             :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    1558             :         const uint8_t inter_direction = me_block_results_ptr->direction;
    1559             :         const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
    1560             :         if (inter_direction == 0) {
    1561             :             for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex) {
    1562             :                 /**************
    1563             :                 NEWMV L0
    1564             :                 ************* */
    1565             :                 if (context_ptr->unipred3x3_injection >= 2) {
    1566             :                     if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1567             :                         continue;
    1568             :                 }
    1569             : 
    1570             :                 int16_t to_inject_mv_x = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1) + BIPRED_3x3_X_POS[bipredIndex])  : ((me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv << 1) + BIPRED_3x3_X_POS[bipredIndex]);
    1571             :                 int16_t to_inject_mv_y = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1) + BIPRED_3x3_Y_POS[bipredIndex])  : ((me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv << 1) + BIPRED_3x3_Y_POS[bipredIndex]);
    1572             :                 uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    1573             : 
    1574             :                 inj_mv = context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE;
    1575             :                 if(umv0tile)
    1576             :                     inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1577             :                 inj_mv = inj_mv && inside_tile;
    1578             :                 if (inj_mv) {
    1579             :                     candidateArray[canTotalCnt].type = INTER_MODE;
    1580             :                     candidateArray[canTotalCnt].distortion_ready = 0;
    1581             :                     candidateArray[canTotalCnt].use_intrabc = 0;
    1582             :                     candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1583             :                     candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    1584             :                     candidateArray[canTotalCnt].inter_mode = NEWMV;
    1585             :                     candidateArray[canTotalCnt].pred_mode = NEWMV;
    1586             :                     candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1587             :                     candidateArray[canTotalCnt].is_compound = 0;
    1588             :                     candidateArray[canTotalCnt].is_new_mv = 1;
    1589             :                     candidateArray[canTotalCnt].is_zero_mv = 0;
    1590             :                     candidateArray[canTotalCnt].drl_index = 0;
    1591             :                     // Set the MV to ME result
    1592             :                     candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x;
    1593             :                     candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y;
    1594             :                     // will be needed later by the rate estimation
    1595             :                     candidateArray[canTotalCnt].ref_mv_index = 0;
    1596             :                     candidateArray[canTotalCnt].pred_mv_weight = 0;
    1597             :                     candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    1598             :                     candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1599             :                     candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    1600             :                     candidateArray[canTotalCnt].transform_type[PLANE_TYPE_Y] = DCT_DCT;
    1601             :                     candidateArray[canTotalCnt].transform_type[PLANE_TYPE_UV] = DCT_DCT;
    1602             : 
    1603             :                     ChooseBestAv1MvPred(
    1604             :                         context_ptr,
    1605             :                         candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1606             :                         context_ptr->cu_ptr,
    1607             :                         candidateArray[canTotalCnt].ref_frame_type,
    1608             :                         candidateArray[canTotalCnt].is_compound,
    1609             :                         candidateArray[canTotalCnt].pred_mode,
    1610             :                         candidateArray[canTotalCnt].motion_vector_xl0,
    1611             :                         candidateArray[canTotalCnt].motion_vector_yl0,
    1612             :                         0, 0,
    1613             :                         &candidateArray[canTotalCnt].drl_index,
    1614             :                         bestPredmv);
    1615             : 
    1616             :                     candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1617             :                     candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1618             :                     INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1619             :                     context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    1620             :                     context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    1621             :                     context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    1622             : 
    1623             :                     ++context_ptr->injected_mv_count_l0;
    1624             :                 }
    1625             :             }
    1626             :         }
    1627             :     }
    1628             : 
    1629             :     // (8 Best_L1 neighbors)
    1630             :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index) {
    1631             :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    1632             :         const uint8_t inter_direction = me_block_results_ptr->direction;
    1633             :         const uint8_t list1_ref_index = me_block_results_ptr->ref_idx_l1;
    1634             :         if (inter_direction == 1) {
    1635             :             for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex) {
    1636             :                 if (isCompoundEnabled) {
    1637             :                     /**************
    1638             :                     NEWMV L1
    1639             :                     ************* */
    1640             :                     if (context_ptr->unipred3x3_injection >= 2) {
    1641             :                         if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1642             :                             continue;
    1643             :                     }
    1644             :                     int16_t to_inject_mv_x = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1) + BIPRED_3x3_X_POS[bipredIndex])  : ((me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].x_mv << 1) + BIPRED_3x3_X_POS[bipredIndex]);
    1645             :                     int16_t to_inject_mv_y = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1) + BIPRED_3x3_Y_POS[bipredIndex])  : ((me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].y_mv << 1) + BIPRED_3x3_Y_POS[bipredIndex]);
    1646             :                     uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    1647             :                     inj_mv = context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE;
    1648             :                     if(umv0tile)
    1649             :                         inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1650             :                     inj_mv = inj_mv && inside_tile;
    1651             :                     if (inj_mv) {
    1652             :                         candidateArray[canTotalCnt].type = INTER_MODE;
    1653             :                         candidateArray[canTotalCnt].distortion_ready = 0;
    1654             :                         candidateArray[canTotalCnt].use_intrabc = 0;
    1655             :                         candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1656             :                         candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)1;
    1657             :                         candidateArray[canTotalCnt].inter_mode = NEWMV;
    1658             :                         candidateArray[canTotalCnt].pred_mode = NEWMV;
    1659             :                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1660             :                         candidateArray[canTotalCnt].is_compound = 0;
    1661             :                         candidateArray[canTotalCnt].is_new_mv = 1;
    1662             :                         candidateArray[canTotalCnt].is_zero_mv = 0;
    1663             :                         candidateArray[canTotalCnt].drl_index = 0;
    1664             :                         // Set the MV to ME result
    1665             :                         candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x;
    1666             :                         candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y;
    1667             :                         // will be needed later by the rate estimation
    1668             :                         candidateArray[canTotalCnt].ref_mv_index = 0;
    1669             :                         candidateArray[canTotalCnt].pred_mv_weight = 0;
    1670             :                         candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    1671             :                         candidateArray[canTotalCnt].ref_frame_index_l0 = -1;
    1672             :                         candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1673             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_Y] = DCT_DCT;
    1674             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_UV] = DCT_DCT;
    1675             : 
    1676             :                         ChooseBestAv1MvPred(
    1677             :                             context_ptr,
    1678             :                             candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1679             :                             context_ptr->cu_ptr,
    1680             :                             candidateArray[canTotalCnt].ref_frame_type,
    1681             :                             candidateArray[canTotalCnt].is_compound,
    1682             :                             candidateArray[canTotalCnt].pred_mode,
    1683             :                             candidateArray[canTotalCnt].motion_vector_xl1,
    1684             :                             candidateArray[canTotalCnt].motion_vector_yl1,
    1685             :                             0, 0,
    1686             :                             &candidateArray[canTotalCnt].drl_index,
    1687             :                             bestPredmv);
    1688             : 
    1689             :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[0].as_mv.col;
    1690             :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[0].as_mv.row;
    1691             :                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1692             :                         context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    1693             :                         context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    1694             :                         context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = to_inject_ref_type;
    1695             :                         ++context_ptr->injected_mv_count_l1;
    1696             :                     }
    1697             :                 }
    1698             :             }
    1699             :         }
    1700             :     }
    1701             :     // update the total number of candidates injected
    1702             :     (*candidateTotalCnt) = canTotalCnt;
    1703             :     return;
    1704             : }
    1705             : 
    1706             : void eighth_pel_bipred_refinement(
    1707             :     const SequenceControlSet *sequence_control_set_ptr,
    1708             :     PictureControlSet        *picture_control_set_ptr,
    1709             :     ModeDecisionContext      *context_ptr,
    1710             :     uint32_t                  me_sb_addr,
    1711             :     SsMeContext              *inloop_me_context,
    1712             :     EbBool                    use_close_loop_me,
    1713             :     uint32_t                  close_loop_me_index,
    1714             :     uint32_t                 *candidateTotalCnt) {
    1715             :     uint32_t                   bipredIndex;
    1716             :     uint32_t                   canTotalCnt = (*candidateTotalCnt);
    1717             :     const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[me_sb_addr];
    1718             :     uint8_t total_me_cnt = me_results->total_me_candidate_index[context_ptr->me_block_offset];
    1719             :     const MeCandidate *me_block_results = me_results->me_candidate[context_ptr->me_block_offset];
    1720             :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    1721             :     EbBool isCompoundEnabled = (picture_control_set_ptr->parent_pcs_ptr->reference_mode == SINGLE_REFERENCE) ? 0 : 1;
    1722             :     IntMv  bestPredmv[2] = { {0}, {0} };
    1723             :     MvReferenceFrame rf[2];
    1724             :     uint8_t inj_mv;
    1725             :     int inside_tile = 1;
    1726             :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    1727             :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    1728             :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    1729             :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    1730             :     if (isCompoundEnabled) {
    1731             :         /**************
    1732             :        NEW_NEWMV
    1733             :        ************* */
    1734             :         for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index) {
    1735             :             const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    1736             :             const uint8_t inter_direction = me_block_results_ptr->direction;
    1737             :             const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
    1738             :             const uint8_t list1_ref_index = me_block_results_ptr->ref_idx_l1;
    1739             : 
    1740             :             if (inter_direction == 2) {
    1741             :                 // (Best_L0, 8 Best_L1 neighbors)
    1742             :                 for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex) {
    1743             :                     if (context_ptr->bipred3x3_injection >= 2) {
    1744             :                         if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1745             :                             continue;
    1746             :                     }
    1747             :                     int16_t to_inject_mv_x_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv << 1;
    1748             :                     int16_t to_inject_mv_y_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv << 1;
    1749             :                     int16_t to_inject_mv_x_l1 = use_close_loop_me ? (((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1) + BIPRED_3x3_X_POS[bipredIndex])) : ((me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].x_mv << 1) + BIPRED_3x3_X_POS[bipredIndex]);
    1750             :                     int16_t to_inject_mv_y_l1 = use_close_loop_me ? (((inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1) + BIPRED_3x3_Y_POS[bipredIndex])) : ((me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].y_mv << 1) + BIPRED_3x3_Y_POS[bipredIndex]);
    1751             :                     rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1752             :                     rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1753             :                     uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    1754             :                     inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE;
    1755             :                     if(umv0tile) {
    1756             :                         inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    1757             :                                       is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1758             :                     }
    1759             :                     inj_mv = inj_mv && inside_tile;
    1760             :                     if (inj_mv) {
    1761             :                         candidateArray[canTotalCnt].type = INTER_MODE;
    1762             :                         candidateArray[canTotalCnt].distortion_ready = 0;
    1763             :                         candidateArray[canTotalCnt].use_intrabc = 0;
    1764             :                         candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1765             :                         candidateArray[canTotalCnt].is_new_mv = 1;
    1766             :                         candidateArray[canTotalCnt].is_zero_mv = 0;
    1767             :                         candidateArray[canTotalCnt].drl_index = 0;
    1768             :                         // Set the MV to ME result
    1769             :                         candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    1770             :                         candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    1771             :                         candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    1772             :                         candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    1773             :                         // will be needed later by the rate estimation
    1774             :                         candidateArray[canTotalCnt].ref_mv_index = 0;
    1775             :                         candidateArray[canTotalCnt].pred_mv_weight = 0;
    1776             :                         candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    1777             :                         candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    1778             :                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1779             :                         candidateArray[canTotalCnt].is_compound = 1;
    1780             :                         candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    1781             :                         rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1782             :                         rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1783             :                         candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    1784             :                         candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1785             :                         candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1786             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_Y] = DCT_DCT;
    1787             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_UV] = DCT_DCT;
    1788             : 
    1789             :                         ChooseBestAv1MvPred(
    1790             :                             context_ptr,
    1791             :                             candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1792             :                             context_ptr->cu_ptr,
    1793             :                             candidateArray[canTotalCnt].ref_frame_type,
    1794             :                             candidateArray[canTotalCnt].is_compound,
    1795             :                             candidateArray[canTotalCnt].pred_mode,
    1796             :                             candidateArray[canTotalCnt].motion_vector_xl0,
    1797             :                             candidateArray[canTotalCnt].motion_vector_yl0,
    1798             :                             candidateArray[canTotalCnt].motion_vector_xl1,
    1799             :                             candidateArray[canTotalCnt].motion_vector_yl1,
    1800             :                             &candidateArray[canTotalCnt].drl_index,
    1801             :                             bestPredmv);
    1802             : 
    1803             :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1804             :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1805             :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    1806             :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    1807             :                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1808             :                         context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    1809             :                         context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    1810             :                         context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    1811             :                         context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    1812             :                         context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    1813             :                         ++context_ptr->injected_mv_count_bipred;
    1814             :                     }
    1815             :                 }
    1816             : 
    1817             :                 // (8 Best_L0 neighbors, Best_L1) :
    1818             :                 for (bipredIndex = 0; bipredIndex < BIPRED_3x3_REFINMENT_POSITIONS; ++bipredIndex)
    1819             :                 {
    1820             :                     if (context_ptr->bipred3x3_injection >= 2) {
    1821             :                         if (ALLOW_REFINEMENT_FLAG[bipredIndex] == 0)
    1822             :                             continue;
    1823             :                     }
    1824             :                     int16_t to_inject_mv_x_l0 = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1) + BIPRED_3x3_X_POS[bipredIndex]) : ((me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv << 1) + BIPRED_3x3_X_POS[bipredIndex]);
    1825             :                     int16_t to_inject_mv_y_l0 = use_close_loop_me ? ((inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1) + BIPRED_3x3_Y_POS[bipredIndex]) : ((me_results->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv << 1) + BIPRED_3x3_Y_POS[bipredIndex]);
    1826             :                     int16_t to_inject_mv_x_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].x_mv << 1;
    1827             :                     int16_t to_inject_mv_y_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].y_mv << 1;
    1828             :                     rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1829             :                     rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1830             :                     uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    1831             :                     inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE;
    1832             :                     if(umv0tile) {
    1833             :                         inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    1834             :                                       is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1835             :                     }
    1836             :                     inj_mv = inj_mv && inside_tile;
    1837             :                     if (inj_mv) {
    1838             :                         candidateArray[canTotalCnt].type = INTER_MODE;
    1839             :                         candidateArray[canTotalCnt].distortion_ready = 0;
    1840             :                         candidateArray[canTotalCnt].use_intrabc = 0;
    1841             :                         candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    1842             :                         candidateArray[canTotalCnt].is_new_mv = 1;
    1843             :                         candidateArray[canTotalCnt].is_zero_mv = 0;
    1844             :                         candidateArray[canTotalCnt].drl_index = 0;
    1845             :                         // Set the MV to ME result
    1846             :                         candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    1847             :                         candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    1848             :                         candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    1849             :                         candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    1850             :                         // will be needed later by the rate estimation
    1851             :                         candidateArray[canTotalCnt].ref_mv_index = 0;
    1852             :                         candidateArray[canTotalCnt].pred_mv_weight = 0;
    1853             :                         candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    1854             :                         candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    1855             :                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    1856             :                         candidateArray[canTotalCnt].is_compound = 1;
    1857             :                         candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    1858             :                         rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    1859             :                         rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    1860             :                         candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    1861             :                         candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    1862             :                         candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    1863             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_Y] = DCT_DCT;
    1864             :                         candidateArray[canTotalCnt].transform_type[PLANE_TYPE_UV] = DCT_DCT;
    1865             : 
    1866             :                         ChooseBestAv1MvPred(
    1867             :                             context_ptr,
    1868             :                             candidateArray[canTotalCnt].md_rate_estimation_ptr,
    1869             :                             context_ptr->cu_ptr,
    1870             :                             candidateArray[canTotalCnt].ref_frame_type,
    1871             :                             candidateArray[canTotalCnt].is_compound,
    1872             :                             candidateArray[canTotalCnt].pred_mode,
    1873             :                             candidateArray[canTotalCnt].motion_vector_xl0,
    1874             :                             candidateArray[canTotalCnt].motion_vector_yl0,
    1875             :                             candidateArray[canTotalCnt].motion_vector_xl1,
    1876             :                             candidateArray[canTotalCnt].motion_vector_yl1,
    1877             :                             &candidateArray[canTotalCnt].drl_index,
    1878             :                             bestPredmv);
    1879             : 
    1880             :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    1881             :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    1882             :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    1883             :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    1884             :                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    1885             :                         context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    1886             :                         context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    1887             :                         context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    1888             :                         context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    1889             :                         context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    1890             :                         ++context_ptr->injected_mv_count_bipred;
    1891             :                     }
    1892             :                 }
    1893             :             }
    1894             :         }
    1895             :     }
    1896             :     // update the total number of candidates injected
    1897             :     (*candidateTotalCnt) = canTotalCnt;
    1898             :     return;
    1899             : }
    1900             : 
    1901             : #endif
    1902             : 
    1903    46447200 : uint8_t GetMaxDrlIndex(uint8_t  refmvCnt, PredictionMode   mode)
    1904             : {
    1905    46447200 :     uint8_t maxDrl = 0;
    1906             : 
    1907    46447200 :     if (mode == NEWMV || mode == NEW_NEWMV) {
    1908    34004600 :         if (refmvCnt < 2)
    1909      755818 :             maxDrl = 1;
    1910    33248800 :         else if (refmvCnt == 2)
    1911    17190000 :             maxDrl = 2;
    1912             :         else
    1913    16058800 :             maxDrl = 3;
    1914             :     }
    1915             : 
    1916    46447200 :     if (mode == NEARMV || mode == NEAR_NEARMV || mode == NEAR_NEWMV || mode == NEW_NEARMV) {
    1917    12470700 :         if (refmvCnt < 3)
    1918     7195520 :             maxDrl = 1;
    1919     5275160 :         else if (refmvCnt == 3)
    1920     2657870 :             maxDrl = 2;
    1921             :         else
    1922     2617290 :             maxDrl = 3;
    1923             :     }
    1924             : 
    1925    46447200 :     return maxDrl;
    1926             : }
    1927             : /*********************************************************************
    1928             : **********************************************************************
    1929             :         Upto 12 inter Candidated injected
    1930             :         Min 6 inter Candidated injected
    1931             : UniPred L0 : NEARST         + upto 3x NEAR
    1932             : UniPred L1 : NEARST         + upto 3x NEAR
    1933             : BIPred     : NEARST_NEARST  + upto 3x NEAR_NEAR
    1934             : **********************************************************************
    1935             : **********************************************************************/
    1936             : #if !II_COMP_FLAG
    1937             : void av1_set_ref_frame(MvReferenceFrame *rf,
    1938             :     int8_t ref_frame_type);
    1939             : #endif
    1940     7842470 : void inject_mvp_candidates_II(
    1941             :     struct ModeDecisionContext     *context_ptr,
    1942             :     PictureControlSet              *picture_control_set_ptr,
    1943             :     CodingUnit                     *cu_ptr,
    1944             :     MvReferenceFrame                 ref_pair,
    1945             :     uint32_t                         *candTotCnt)
    1946             : {
    1947     7842470 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    1948     7842470 :     EbBool allow_compound = (frm_hdr->reference_mode == SINGLE_REFERENCE || context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) ? EB_FALSE : EB_TRUE;
    1949             :     uint8_t inj_mv;
    1950     7842470 :     uint32_t                   canIdx = *candTotCnt;
    1951     7842470 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    1952     7842470 :     MacroBlockD  *xd = cu_ptr->av1xd;
    1953             :     uint8_t drli, maxDrlIndex;
    1954             :     IntMv    nearestmv[2], nearmv[2], ref_mv[2];
    1955             : 
    1956             :     MvReferenceFrame rf[2];
    1957     7842470 :     int inside_tile = 1;
    1958     7842470 :     SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    1959     7842470 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    1960     7842470 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    1961     7842470 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    1962     7842470 :     av1_set_ref_frame(rf, ref_pair);
    1963             :     MD_COMP_TYPE cur_type; //MVP
    1964             : #if FIX_COMPOUND
    1965     7842610 :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    1966     7842610 :     MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    1967     7850530 :     MD_COMP_TYPE tot_comp_types = (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    1968             :         (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    1969        7916 :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;//MD_COMP_DIST;// MD_COMP_AVG;//
    1970             : #else
    1971             :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    1972             : #endif
    1973             : #if SPEED_OPT
    1974     7842610 :     if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    1975     1980110 :         tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    1976             : #endif
    1977             :  #if II_COMP_FLAG && !FIX_COMPOUND
    1978             :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    1979             : #endif
    1980             :     //single ref/list
    1981     7842610 :     if (rf[1] == NONE_FRAME)
    1982             :     {
    1983     3214050 :         MvReferenceFrame frame_type = rf[0];
    1984     3214050 :         uint8_t list_idx = get_list_idx(rf[0]);
    1985     3213990 :         uint8_t ref_idx = get_ref_frame_idx(rf[0]);
    1986             : 
    1987             :         //NEAREST
    1988     3214250 :         int16_t to_inject_mv_x = context_ptr->cu_ptr->ref_mvs[frame_type][0].as_mv.col;
    1989     3214250 :         int16_t to_inject_mv_y = context_ptr->cu_ptr->ref_mvs[frame_type][0].as_mv.row;
    1990             : 
    1991     6428470 :         inj_mv = list_idx == 0 ?
    1992     1954380 :             context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, frame_type) == EB_FALSE :
    1993     1259860 :             context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, frame_type) == EB_FALSE;
    1994             : 
    1995     3214220 :         if(umv0tile)
    1996           0 :             inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    1997     3214020 :         inj_mv = inj_mv && inside_tile;
    1998     3214020 :         if (inj_mv) {
    1999             : #if II_COMP_FLAG // NEARESTMV
    2000             :             uint8_t inter_type;
    2001     3214020 :             uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEARESTMV, rf);
    2002     3213970 :             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    2003             : #if OBMC_FLAG
    2004     3213970 :             uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEARESTMV) == OBMC_CAUSAL;
    2005     3213920 :             tot_inter_types = is_obmc_allowed ? tot_inter_types + 1 : tot_inter_types;
    2006             : #endif
    2007    11057200 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    2008             :             {
    2009             : #endif
    2010     7844060 :             candidateArray[canIdx].type = INTER_MODE;
    2011     7844060 :             candidateArray[canIdx].inter_mode = NEARESTMV;
    2012     7844060 :             candidateArray[canIdx].pred_mode = NEARESTMV;
    2013     7844060 :             candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2014     7844060 :             candidateArray[canIdx].is_compound = 0;
    2015     7844060 :             candidateArray[canIdx].distortion_ready = 0;
    2016     7844060 :             candidateArray[canIdx].use_intrabc = 0;
    2017     7844060 :             candidateArray[canIdx].merge_flag = EB_FALSE;
    2018     7844060 :             candidateArray[canIdx].prediction_direction[0] = list_idx;
    2019     7844060 :             candidateArray[canIdx].is_new_mv = 0;
    2020     7844060 :             candidateArray[canIdx].is_zero_mv = 0;
    2021             : 
    2022     7844060 :             candidateArray[canIdx].drl_index = 0;
    2023     7844060 :             candidateArray[canIdx].ref_mv_index = 0;
    2024     7844060 :             candidateArray[canIdx].pred_mv_weight = 0;
    2025     7844060 :             candidateArray[canIdx].ref_frame_type = frame_type;
    2026             : 
    2027     7844060 :             candidateArray[canIdx].ref_frame_index_l0 = (list_idx == 0) ? ref_idx : -1;
    2028     7844060 :             candidateArray[canIdx].ref_frame_index_l1 = (list_idx == 1) ? ref_idx : -1;
    2029     7844060 :             candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2030     7844060 :             candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2031     7844060 :             if (list_idx == 0) {
    2032     4789980 :                 candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x;
    2033     4789980 :                 candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y;
    2034     4789980 :                 context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    2035     4789980 :                 context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    2036     4789980 :                 context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = frame_type;
    2037     4789980 :                 ++context_ptr->injected_mv_count_l0;
    2038             :             }
    2039             :             else {
    2040     3054080 :                 candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x;
    2041     3054080 :                 candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y;
    2042     3054080 :                 context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    2043     3054080 :                 context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    2044     3054080 :                 context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = frame_type;
    2045     3054080 :                 ++context_ptr->injected_mv_count_l1;
    2046             :             }
    2047             : #if II_COMP_FLAG
    2048     7844060 :             if (inter_type == 0) {
    2049     3214130 :                 candidateArray[canIdx].is_interintra_used = 0;
    2050     3214130 :                 candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2051             :             }
    2052             :             else {
    2053     4629920 :                 if (is_ii_allowed) {
    2054     4118340 :                     if (inter_type == 1) {
    2055     1374880 :                         inter_intra_search(
    2056             :                             picture_control_set_ptr,
    2057             :                             context_ptr,
    2058     1374880 :                             &candidateArray[canIdx]);
    2059     1374850 :                         candidateArray[canIdx].is_interintra_used = 1;
    2060     1374850 :                         candidateArray[canIdx].use_wedge_interintra = 1;
    2061     1374850 :                         candidateArray[canIdx].ii_wedge_sign = 0;
    2062             :                     }
    2063     2743470 :                     else if (inter_type == 2) {
    2064     1374860 :                         candidateArray[canIdx].is_interintra_used = 1;
    2065     1374860 :                         candidateArray[canIdx].interintra_mode = candidateArray[canIdx - 1].interintra_mode;
    2066     1374860 :                         candidateArray[canIdx].use_wedge_interintra = 0;
    2067             :                     }
    2068             :                 }
    2069             : #if OBMC_FLAG
    2070     4629900 :                 if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    2071     1880810 :                     candidateArray[canIdx].is_interintra_used = 0;
    2072     1880810 :                     candidateArray[canIdx].motion_mode = OBMC_CAUSAL;
    2073             :                 }
    2074             : #endif
    2075             :             }
    2076             : #endif
    2077     7844030 :             INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2078             : #if II_COMP_FLAG
    2079             :             }
    2080             : #endif
    2081             :         }
    2082             : 
    2083             :         //NEAR
    2084     3213160 :         maxDrlIndex = GetMaxDrlIndex(xd->ref_mv_count[frame_type], NEARMV);
    2085             : 
    2086     8530600 :         for (drli = 0; drli < maxDrlIndex; drli++)
    2087             :         {
    2088     5318380 :             get_av1_mv_pred_drl(
    2089             :                 context_ptr,
    2090             :                 cu_ptr,
    2091             :                 frame_type,
    2092             :                 0,
    2093             :                 NEARMV,
    2094             :                 drli,
    2095             :                 nearestmv,
    2096             :                 nearmv,
    2097             :                 ref_mv);
    2098             : 
    2099     5318180 :             int16_t to_inject_mv_x = nearmv[0].as_mv.col;
    2100     5318180 :             int16_t to_inject_mv_y = nearmv[0].as_mv.row;
    2101             : 
    2102    10636500 :             inj_mv = list_idx == 0 ?
    2103     3213560 :                 context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, frame_type) == EB_FALSE :
    2104     2104620 :                 context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, frame_type) == EB_FALSE;
    2105             : 
    2106     5318280 :             if(umv0tile)
    2107           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2108     5318420 :             inj_mv = inj_mv && inside_tile;
    2109     5318420 :             if (inj_mv) {
    2110             : #if II_COMP_FLAG // NEARMV
    2111             :             uint8_t inter_type;
    2112     5298560 :             uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEARMV, rf);
    2113     5298410 :             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    2114             : #if OBMC_FLAG
    2115     5298410 :             uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEARMV) == OBMC_CAUSAL;
    2116     5298360 :             tot_inter_types = is_obmc_allowed ? tot_inter_types+1 : tot_inter_types;
    2117             : #endif
    2118             : 
    2119    19005900 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    2120             :             {
    2121             : #endif
    2122    13709200 :                 candidateArray[canIdx].type = INTER_MODE;
    2123    13709200 :                 candidateArray[canIdx].inter_mode = NEARMV;
    2124    13709200 :                 candidateArray[canIdx].pred_mode = NEARMV;
    2125    13709200 :                 candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2126    13709200 :                 candidateArray[canIdx].is_compound = 0;
    2127    13709200 :                 candidateArray[canIdx].distortion_ready = 0;
    2128    13709200 :                 candidateArray[canIdx].use_intrabc = 0;
    2129    13709200 :                 candidateArray[canIdx].merge_flag = EB_FALSE;
    2130    13709200 :                 candidateArray[canIdx].prediction_direction[0] = list_idx;
    2131    13709200 :                 candidateArray[canIdx].is_new_mv = 0;
    2132    13709200 :                 candidateArray[canIdx].is_zero_mv = 0;
    2133    13709200 :                 candidateArray[canIdx].drl_index = drli;
    2134    13709200 :                 candidateArray[canIdx].ref_mv_index = 0;
    2135    13709200 :                 candidateArray[canIdx].pred_mv_weight = 0;
    2136    13709200 :                 candidateArray[canIdx].ref_frame_type = frame_type;
    2137             : 
    2138    13709200 :                 candidateArray[canIdx].ref_frame_index_l0 = (list_idx == 0) ? ref_idx : -1;
    2139    13709200 :                 candidateArray[canIdx].ref_frame_index_l1 = (list_idx == 1) ? ref_idx : -1;
    2140             : 
    2141    13709200 :                 candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2142    13709200 :                 candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2143    13709200 :                 if (list_idx == 0) {
    2144     8309270 :                     candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x;
    2145     8309270 :                     candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y;
    2146     8309270 :                     context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    2147     8309270 :                     context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    2148     8309270 :                     context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = frame_type;
    2149     8309270 :                     ++context_ptr->injected_mv_count_l0;
    2150             :                 }
    2151             :                 else {
    2152     5399890 :                     candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x;
    2153     5399890 :                     candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y;
    2154     5399890 :                     context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    2155     5399890 :                     context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    2156     5399890 :                     context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = frame_type;
    2157     5399890 :                     ++context_ptr->injected_mv_count_l1;
    2158             :                 }
    2159             : #if II_COMP_FLAG
    2160    13709200 :             if (inter_type == 0) {
    2161     5298980 :                 candidateArray[canIdx].is_interintra_used = 0;
    2162     5298980 :                 candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2163             :             }
    2164             :             else {
    2165     8410180 :                 if (is_ii_allowed) {
    2166     7440450 :                     if (inter_type == 1) {
    2167     2481340 :                         inter_intra_search(
    2168             :                             picture_control_set_ptr,
    2169             :                             context_ptr,
    2170     2481340 :                             &candidateArray[canIdx]);
    2171     2481260 :                         candidateArray[canIdx].is_interintra_used = 1;
    2172     2481260 :                         candidateArray[canIdx].use_wedge_interintra = 1;
    2173     2481260 :                         candidateArray[canIdx].ii_wedge_sign = 0;
    2174             :                     }
    2175     4959110 :                     else if (inter_type == 2) {
    2176     2481300 :                         candidateArray[canIdx].is_interintra_used = 1;
    2177     2481300 :                         candidateArray[canIdx].interintra_mode = candidateArray[canIdx - 1].interintra_mode;
    2178     2481300 :                         candidateArray[canIdx].use_wedge_interintra = 0;
    2179             :                     }
    2180             :                 }
    2181             : #if OBMC_FLAG
    2182     8410090 :                 if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    2183     3449020 :                     candidateArray[canIdx].is_interintra_used = 0;
    2184     3449020 :                     candidateArray[canIdx].motion_mode = OBMC_CAUSAL;
    2185             :                 }
    2186             : #endif
    2187             :             }
    2188             : #endif
    2189    13709100 :                 INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2190             : #if II_COMP_FLAG
    2191             :             }
    2192             : #endif
    2193             :             }
    2194             :         }
    2195             :     }
    2196     4628560 :     else if (allow_compound)
    2197             :     {
    2198     2937640 :         uint8_t ref_idx_0 = get_ref_frame_idx(rf[0]);
    2199     2937630 :         uint8_t ref_idx_1 = get_ref_frame_idx(rf[1]);
    2200             : 
    2201             :         {
    2202             :             //NEAREST_NEAREST
    2203     2937690 :             int16_t to_inject_mv_x_l0 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].this_mv.as_mv.col;
    2204     2937690 :             int16_t to_inject_mv_y_l0 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].this_mv.as_mv.row;
    2205     2937690 :             int16_t to_inject_mv_x_l1 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.col;
    2206     2937690 :             int16_t to_inject_mv_y_l1 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.row;
    2207             : 
    2208     2937690 :             inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2209             : 
    2210     2937640 :             if(umv0tile) {
    2211           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    2212           0 :                               is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2213             :             }
    2214     2937640 :             inj_mv = inj_mv && inside_tile;
    2215     2937640 :             if (inj_mv) {
    2216             : 
    2217     2937650 :                 context_ptr->variance_ready = 0;
    2218    13180000 :                 for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    2219             :                 {
    2220    10242900 :                     if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    2221             :                     // If two predictors are very similar, skip wedge compound mode search
    2222    10242900 :                     if (context_ptr->variance_ready)
    2223           0 :                         if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEAREST_NEARESTMV) && context_ptr->prediction_mse < 64))
    2224           0 :                             continue;
    2225    10242900 :                 candidateArray[canIdx].type = INTER_MODE;
    2226    10242900 :                 candidateArray[canIdx].inter_mode = NEAREST_NEARESTMV;
    2227    10242900 :                 candidateArray[canIdx].pred_mode = NEAREST_NEARESTMV;
    2228    10242900 :                 candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2229    10242900 :                 candidateArray[canIdx].is_compound = 1;
    2230             : #if II_COMP_FLAG
    2231    10242900 :                 candidateArray[canIdx].is_interintra_used = 0;
    2232             : #endif
    2233    10242900 :                 candidateArray[canIdx].distortion_ready = 0;
    2234    10242900 :                 candidateArray[canIdx].use_intrabc = 0;
    2235             : 
    2236    10242900 :                 candidateArray[canIdx].merge_flag =
    2237     2937640 :                     cur_type == MD_COMP_AVG &&
    2238     2937640 :                     picture_control_set_ptr->parent_pcs_ptr->is_skip_mode_allowed &&
    2239     2899430 :                     (rf[0] == frm_hdr->skip_mode_params.ref_frame_idx_0 + 1) &&
    2240    13180600 :                     (rf[1] == frm_hdr->skip_mode_params.ref_frame_idx_1 + 1) ? EB_TRUE : EB_FALSE;
    2241             : 
    2242    10242900 :                 candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2243    10242900 :                 candidateArray[canIdx].is_new_mv = 0;
    2244    10242900 :                 candidateArray[canIdx].is_zero_mv = 0;
    2245    10242900 :                 candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2246    10242900 :                 candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2247    10242900 :                 candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2248    10242900 :                 candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2249    10242900 :                 candidateArray[canIdx].drl_index = 0;
    2250    10242900 :                 candidateArray[canIdx].ref_mv_index = 0;
    2251    10242900 :                 candidateArray[canIdx].pred_mv_weight = 0;
    2252    10242900 :                 candidateArray[canIdx].ref_frame_type = ref_pair;
    2253    10242900 :                 candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2254    10242900 :                 candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2255             : 
    2256    10242900 :                 candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2257    10242900 :                 candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2258             : 
    2259    10242900 :                 context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2260    10242900 :                 context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2261    10242900 :                 context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2262    10242900 :                 context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2263    10242900 :                 context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2264    10242900 :                 ++context_ptr->injected_mv_count_bipred;
    2265             :                 //NRST-NRST
    2266    10242900 :                 determine_compound_mode(
    2267             :                     picture_control_set_ptr,
    2268             :                     context_ptr,
    2269    10242900 :                     &candidateArray[canIdx],
    2270             :                     cur_type);
    2271    10242300 :                 INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2272             :                 }
    2273             :             }
    2274             : 
    2275             :             //NEAR_NEAR
    2276     2937040 :             maxDrlIndex = GetMaxDrlIndex(xd->ref_mv_count[ref_pair], NEAR_NEARMV);
    2277     7592140 :             for (drli = 0; drli < maxDrlIndex; drli++) {
    2278     4654690 :                 get_av1_mv_pred_drl(
    2279             :                     context_ptr,
    2280             :                     cu_ptr,
    2281             :                     ref_pair,
    2282             :                     1,
    2283             :                     NEAR_NEARMV,
    2284             :                     drli,
    2285             :                     nearestmv,
    2286             :                     nearmv,
    2287             :                     ref_mv);
    2288             : 
    2289     4654740 :                 int16_t to_inject_mv_x_l0 = nearmv[0].as_mv.col;
    2290     4654740 :                 int16_t to_inject_mv_y_l0 = nearmv[0].as_mv.row;
    2291     4654740 :                 int16_t to_inject_mv_x_l1 = nearmv[1].as_mv.col;
    2292     4654740 :                 int16_t to_inject_mv_y_l1 = nearmv[1].as_mv.row;
    2293             : 
    2294     4654740 :                 inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2295             : 
    2296     4654960 :                 if(umv0tile) {
    2297           0 :                     inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    2298           0 :                                   is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2299             :                 }
    2300     4654960 :                 inj_mv = inj_mv && inside_tile;
    2301     4654960 :                 if (inj_mv) {
    2302             : 
    2303     4578410 :                     context_ptr->variance_ready = 0;
    2304    20766000 :                     for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    2305             :                     {
    2306    16188000 :                         if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    2307             :                         // If two predictors are very similar, skip wedge compound mode search
    2308    16188000 :                         if (context_ptr->variance_ready)
    2309           0 :                             if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEAR_NEARMV) && context_ptr->prediction_mse < 64))
    2310           0 :                                 continue;
    2311    16188000 :                     candidateArray[canIdx].type = INTER_MODE;
    2312    16188000 :                     candidateArray[canIdx].inter_mode = NEAR_NEARMV;
    2313    16188000 :                     candidateArray[canIdx].pred_mode = NEAR_NEARMV;
    2314    16188000 :                     candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2315    16188000 :                     candidateArray[canIdx].is_compound = 1;
    2316             : #if II_COMP_FLAG
    2317    16188000 :                     candidateArray[canIdx].is_interintra_used = 0;
    2318             : #endif
    2319    16188000 :                     candidateArray[canIdx].distortion_ready = 0;
    2320    16188000 :                     candidateArray[canIdx].use_intrabc = 0;
    2321    16188000 :                     candidateArray[canIdx].merge_flag = EB_FALSE;
    2322    16188000 :                     candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2323    16188000 :                     candidateArray[canIdx].is_new_mv = 0;
    2324    16188000 :                     candidateArray[canIdx].is_zero_mv = 0;
    2325             : 
    2326    16188000 :                     candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2327    16188000 :                     candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2328    16188000 :                     candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2329    16188000 :                     candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2330             : 
    2331    16188000 :                     candidateArray[canIdx].drl_index = drli;
    2332    16188000 :                     candidateArray[canIdx].ref_mv_index = 0;
    2333    16188000 :                     candidateArray[canIdx].pred_mv_weight = 0;
    2334             : 
    2335    16188000 :                     candidateArray[canIdx].ref_frame_type = ref_pair;
    2336             : 
    2337    16188000 :                     candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2338    16188000 :                     candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2339             : 
    2340    16188000 :                     candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2341    16188000 :                     candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2342             : 
    2343    16188000 :                     context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2344    16188000 :                     context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2345    16188000 :                     context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2346    16188000 :                     context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2347    16188000 :                     context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2348    16188000 :                     ++context_ptr->injected_mv_count_bipred;
    2349             :                     //NR-NR
    2350    16188000 :                     determine_compound_mode(
    2351             :                         picture_control_set_ptr,
    2352             :                         context_ptr,
    2353    16188000 :                         &candidateArray[canIdx],
    2354             :                         cur_type);
    2355    16187600 :                     INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2356             :                     }
    2357             :                 }
    2358             : 
    2359             :             }
    2360             :         }
    2361             :     }
    2362             : 
    2363             :     //update tot Candidate count
    2364     7840600 :     *candTotCnt = canIdx;
    2365     7840600 : }
    2366             : 
    2367     2544290 : void inject_new_nearest_new_comb_candidates(
    2368             :     const SequenceControlSet       *sequence_control_set_ptr,
    2369             :     struct ModeDecisionContext     *context_ptr,
    2370             :     PictureControlSet              *picture_control_set_ptr,
    2371             :     MvReferenceFrame                ref_pair,
    2372             :     uint32_t                       *candTotCnt)
    2373             : {
    2374             :     uint8_t inj_mv;
    2375     2544290 :     uint32_t                  canIdx = *candTotCnt;
    2376     2544290 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    2377     2544290 :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    2378             : #if FIX_NEAREST_NEW
    2379             :     IntMv    nearestmv[2], nearmv[2], ref_mv[2];
    2380             : #endif
    2381             : #if INJECT_NEW_NEAR_NEAR_NEW
    2382             :     uint8_t drli, maxDrlIndex;
    2383             : #endif
    2384     2544290 :     int inside_tile = 1;
    2385     2544290 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    2386     2544290 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    2387     2544290 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    2388             : 
    2389             :     MvReferenceFrame rf[2];
    2390     2544290 :     av1_set_ref_frame(rf, ref_pair);
    2391             :     MD_COMP_TYPE cur_type; //N_NR N_NRST
    2392             : #if FIX_COMPOUND
    2393     2544290 :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    2394     2544290 :     MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    2395     2544290 :     MD_COMP_TYPE tot_comp_types = (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    2396             :         (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    2397           0 :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;//MD_COMP_DIST;// MD_COMP_AVG;//
    2398             : #else
    2399             :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    2400             : #endif
    2401             : #if SPEED_OPT
    2402     2544290 :     if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    2403      741460 :         tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    2404             : #endif
    2405             : 
    2406             :     {
    2407     2544290 :         uint8_t ref_idx_0 = get_ref_frame_idx(rf[0]);
    2408     2544280 :         uint8_t ref_idx_1 = get_ref_frame_idx(rf[1]);
    2409             : 
    2410     2544270 :         if (rf[1] != NONE_FRAME)
    2411             :         {
    2412             :             {
    2413             :                 //NEAREST_NEWMV
    2414     1488840 :                 const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[context_ptr->me_sb_addr];
    2415             : 
    2416     1488840 :                 int16_t to_inject_mv_x_l0 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].this_mv.as_mv.col;
    2417     1488840 :                 int16_t to_inject_mv_y_l0 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].this_mv.as_mv.row;
    2418     1488840 :                 int16_t to_inject_mv_x_l1 = me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (get_list_idx(rf[1]) << 2) : (get_list_idx(rf[1]) << 1)) + ref_idx_1].x_mv << 1;
    2419     1488840 :                 int16_t to_inject_mv_y_l1 = me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (get_list_idx(rf[1]) << 2) : (get_list_idx(rf[1]) << 1)) + ref_idx_1].y_mv << 1;
    2420             : 
    2421     1488820 :                 inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2422             : 
    2423     1488800 :                 if(umv0tile) {
    2424           0 :                     inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    2425           0 :                                   is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2426             :                 }
    2427     1488800 :                 inj_mv = inj_mv && inside_tile;
    2428     1488800 :                 if (inj_mv) {
    2429             : 
    2430     1457350 :                     context_ptr->variance_ready = 0;
    2431     6612000 :                     for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    2432             :                     {
    2433     5154630 :                         if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    2434             :                         // If two predictors are very similar, skip wedge compound mode search
    2435     5154630 :                         if (context_ptr->variance_ready)
    2436           0 :                             if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEAREST_NEWMV) && context_ptr->prediction_mse < 64))
    2437           0 :                                 continue;
    2438     5154630 :                     candidateArray[canIdx].type = INTER_MODE;
    2439     5154630 :                     candidateArray[canIdx].inter_mode = NEAREST_NEWMV;
    2440     5154630 :                     candidateArray[canIdx].pred_mode = NEAREST_NEWMV;
    2441     5154630 :                     candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2442     5154630 :                     candidateArray[canIdx].is_compound = 1;
    2443             : #if II_COMP_FLAG
    2444     5154630 :                     candidateArray[canIdx].is_interintra_used = 0;
    2445             : #endif
    2446     5154630 :                     candidateArray[canIdx].distortion_ready = 0;
    2447     5154630 :                     candidateArray[canIdx].use_intrabc = 0;
    2448             : 
    2449     5154630 :                     candidateArray[canIdx].merge_flag = EB_FALSE;
    2450             : 
    2451     5154630 :                     candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2452     5154630 :                     candidateArray[canIdx].is_new_mv = 0;
    2453     5154630 :                     candidateArray[canIdx].is_zero_mv = 0;
    2454     5154630 :                     candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2455     5154630 :                     candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2456     5154630 :                     candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2457     5154630 :                     candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2458     5154630 :                     candidateArray[canIdx].drl_index = 0;
    2459     5154630 :                     candidateArray[canIdx].ref_mv_index = 0;
    2460     5154630 :                     candidateArray[canIdx].pred_mv_weight = 0;
    2461     5154630 :                     candidateArray[canIdx].ref_frame_type = ref_pair;
    2462     5154630 :                     candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2463     5154630 :                     candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2464     5154630 :                     candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2465     5154630 :                     candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2466             : #if FIX_NEAREST_NEW
    2467     5154630 :                     get_av1_mv_pred_drl(
    2468             :                         context_ptr,
    2469             :                         context_ptr->cu_ptr,
    2470     5154630 :                         candidateArray[canIdx].ref_frame_type,
    2471     5154630 :                         candidateArray[canIdx].is_compound,
    2472             :                         NEAREST_NEWMV,
    2473             :                         0,//not needed drli,
    2474             :                         nearestmv,
    2475             :                         nearmv,
    2476             :                         ref_mv);
    2477     5154800 :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_1] = ref_mv[1].as_mv.col;
    2478     5154800 :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_1] = ref_mv[1].as_mv.row;
    2479             : #else
    2480             :                     IntMv  bestPredmv[2] = { {0}, {0} };
    2481             : 
    2482             :                     ChooseBestAv1MvPred(
    2483             :                         context_ptr,
    2484             :                         candidateArray[canIdx].md_rate_estimation_ptr,
    2485             :                         context_ptr->cu_ptr,
    2486             :                         candidateArray[canIdx].ref_frame_type,
    2487             :                         candidateArray[canIdx].is_compound,
    2488             :                         candidateArray[canIdx].pred_mode,
    2489             :                         candidateArray[canIdx].motion_vector_xl0,
    2490             :                         candidateArray[canIdx].motion_vector_yl0,
    2491             :                         candidateArray[canIdx].motion_vector_xl1,
    2492             :                         candidateArray[canIdx].motion_vector_yl1,
    2493             :                         &candidateArray[canIdx].drl_index,
    2494             :                         bestPredmv);
    2495             : 
    2496             :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    2497             :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    2498             :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    2499             :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    2500             : #endif
    2501     5154800 :                     context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2502     5154800 :                     context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2503     5154800 :                     context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2504     5154800 :                     context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2505     5154800 :                     context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2506     5154800 :                     ++context_ptr->injected_mv_count_bipred;
    2507             :                     //NRST_N
    2508     5154800 :                     determine_compound_mode(
    2509             :                         picture_control_set_ptr,
    2510             :                         context_ptr,
    2511     5154800 :                         &candidateArray[canIdx],
    2512             :                         cur_type);
    2513     5154630 :                     INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2514             :                     }
    2515             :                 }
    2516             :             }
    2517             : 
    2518             :             {
    2519             :                 //NEW_NEARESTMV
    2520     1488810 :                 const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[context_ptr->me_sb_addr];
    2521             : 
    2522     1488810 :                 int16_t to_inject_mv_x_l0 = me_results->me_mv_array[context_ptr->me_block_offset][ref_idx_0].x_mv << 1;
    2523     1488810 :                 int16_t to_inject_mv_y_l0 = me_results->me_mv_array[context_ptr->me_block_offset][ref_idx_0].y_mv << 1;
    2524     1488810 :                 int16_t to_inject_mv_x_l1 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.col;
    2525     1488810 :                 int16_t to_inject_mv_y_l1 = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.row;
    2526             : 
    2527     1488810 :                 inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2528             : 
    2529     1488770 :                 if(umv0tile) {
    2530           0 :                     inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    2531           0 :                                   is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2532             :                 }
    2533     1488770 :                 inj_mv = inj_mv && inside_tile;
    2534     1488770 :                 if (inj_mv)
    2535             :                 {
    2536             : 
    2537     1443960 :                     context_ptr->variance_ready = 0;
    2538     6549580 :                     for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    2539             :                     {
    2540     5105580 :                         if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    2541             :                         // If two predictors are very similar, skip wedge compound mode search
    2542     5105580 :                         if (context_ptr->variance_ready)
    2543           0 :                             if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEARESTMV) && context_ptr->prediction_mse < 64))
    2544           0 :                                 continue;
    2545     5105580 :                     candidateArray[canIdx].type = INTER_MODE;
    2546     5105580 :                     candidateArray[canIdx].inter_mode = NEW_NEARESTMV;
    2547     5105580 :                     candidateArray[canIdx].pred_mode = NEW_NEARESTMV;
    2548     5105580 :                     candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2549     5105580 :                     candidateArray[canIdx].is_compound = 1;
    2550             : #if II_COMP_FLAG
    2551     5105580 :                     candidateArray[canIdx].is_interintra_used = 0;
    2552             : #endif
    2553     5105580 :                     candidateArray[canIdx].distortion_ready = 0;
    2554     5105580 :                     candidateArray[canIdx].use_intrabc = 0;
    2555             : 
    2556     5105580 :                     candidateArray[canIdx].merge_flag = EB_FALSE;
    2557             : 
    2558     5105580 :                     candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2559     5105580 :                     candidateArray[canIdx].is_new_mv = 0;
    2560     5105580 :                     candidateArray[canIdx].is_zero_mv = 0;
    2561     5105580 :                     candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2562     5105580 :                     candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2563     5105580 :                     candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2564     5105580 :                     candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2565     5105580 :                     candidateArray[canIdx].drl_index = 0;
    2566     5105580 :                     candidateArray[canIdx].ref_mv_index = 0;
    2567     5105580 :                     candidateArray[canIdx].pred_mv_weight = 0;
    2568     5105580 :                     candidateArray[canIdx].ref_frame_type = ref_pair;
    2569     5105580 :                     candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2570     5105580 :                     candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2571     5105580 :                     candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2572     5105580 :                     candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2573             : #if FIX_NEAREST_NEW
    2574     5105580 :                     get_av1_mv_pred_drl(
    2575             :                         context_ptr,
    2576             :                         context_ptr->cu_ptr,
    2577     5105580 :                         candidateArray[canIdx].ref_frame_type,
    2578     5105580 :                         candidateArray[canIdx].is_compound,
    2579             :                         NEW_NEARESTMV,
    2580             :                         0,//not needed drli,
    2581             :                         nearestmv,
    2582             :                         nearmv,
    2583             :                         ref_mv);
    2584     5105720 :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_0] = ref_mv[0].as_mv.col;
    2585     5105720 :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_0] = ref_mv[0].as_mv.row;
    2586             : #else
    2587             :                     IntMv  bestPredmv[2] = { {0}, {0} };
    2588             : 
    2589             :                     ChooseBestAv1MvPred(
    2590             :                         context_ptr,
    2591             :                         candidateArray[canIdx].md_rate_estimation_ptr,
    2592             :                         context_ptr->cu_ptr,
    2593             :                         candidateArray[canIdx].ref_frame_type,
    2594             :                         candidateArray[canIdx].is_compound,
    2595             :                         candidateArray[canIdx].pred_mode,
    2596             :                         candidateArray[canIdx].motion_vector_xl0,
    2597             :                         candidateArray[canIdx].motion_vector_yl0,
    2598             :                         candidateArray[canIdx].motion_vector_xl1,
    2599             :                         candidateArray[canIdx].motion_vector_yl1,
    2600             :                         &candidateArray[canIdx].drl_index,
    2601             :                         bestPredmv);
    2602             : 
    2603             :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    2604             :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    2605             :                     candidateArray[canIdx].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    2606             :                     candidateArray[canIdx].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    2607             : #endif
    2608     5105720 :                     context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2609     5105720 :                     context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2610     5105720 :                     context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2611     5105720 :                     context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2612     5105720 :                     context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2613     5105720 :                     ++context_ptr->injected_mv_count_bipred;
    2614             :                     //N_NRST
    2615     5105720 :                     determine_compound_mode(
    2616             :                         picture_control_set_ptr,
    2617             :                         context_ptr,
    2618     5105720 :                         &candidateArray[canIdx],
    2619             :                         cur_type);
    2620     5105620 :                     INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2621             :                     }
    2622             :                 }
    2623             :             }
    2624             : #if INJECT_NEW_NEAR_NEAR_NEW
    2625             :              //NEW_NEARMV
    2626             :             {
    2627     1488810 :                 maxDrlIndex = GetMaxDrlIndex(xd->ref_mv_count[ref_pair], NEW_NEARMV);
    2628             : 
    2629     3823840 :                 for (drli = 0; drli < maxDrlIndex; drli++) {
    2630     2335220 :                     get_av1_mv_pred_drl(
    2631             :                         context_ptr,
    2632             :                         context_ptr->cu_ptr,
    2633             :                         ref_pair,
    2634             :                         1,
    2635             :                         NEW_NEARMV,
    2636             :                         drli,
    2637             :                         nearestmv,
    2638             :                         nearmv,
    2639             :                         ref_mv);
    2640             : 
    2641             :                         //NEW_NEARMV
    2642     2335270 :                         const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[context_ptr->me_sb_addr];
    2643             : 
    2644     2335270 :                         int16_t to_inject_mv_x_l0 = me_results->me_mv_array[context_ptr->me_block_offset][ref_idx_0].x_mv << 1;
    2645     2335270 :                         int16_t to_inject_mv_y_l0 = me_results->me_mv_array[context_ptr->me_block_offset][ref_idx_0].y_mv << 1;
    2646     2335270 :                         int16_t to_inject_mv_x_l1 = nearmv[1].as_mv.col;
    2647     2335270 :                         int16_t to_inject_mv_y_l1 = nearmv[1].as_mv.row;
    2648             : 
    2649     2335270 :                         inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2650             : 
    2651     2335340 :                         if (inj_mv){
    2652     2226060 :                             context_ptr->variance_ready = 0 ;
    2653    10152300 :                             for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++){
    2654             :                                 // If two predictors are very similar, skip wedge compound mode search
    2655     7926550 :                                 if (context_ptr->variance_ready)
    2656           0 :                                     if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEARMV) && context_ptr->prediction_mse < 64))
    2657           0 :                                         continue;
    2658             : 
    2659     7926550 :                                 candidateArray[canIdx].type = INTER_MODE;
    2660     7926550 :                                 candidateArray[canIdx].inter_mode = NEW_NEARMV;
    2661     7926550 :                                 candidateArray[canIdx].pred_mode = NEW_NEARMV;
    2662     7926550 :                                 candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2663     7926550 :                                 candidateArray[canIdx].is_compound = 1;
    2664     7926550 :                                 candidateArray[canIdx].is_interintra_used = 0;
    2665             : 
    2666     7926550 :                                 candidateArray[canIdx].distortion_ready = 0;
    2667     7926550 :                                 candidateArray[canIdx].use_intrabc = 0;
    2668     7926550 :                                 candidateArray[canIdx].merge_flag = EB_FALSE;
    2669             : 
    2670     7926550 :                                 candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2671     7926550 :                                 candidateArray[canIdx].is_new_mv = 0;
    2672     7926550 :                                 candidateArray[canIdx].is_zero_mv = 0;
    2673     7926550 :                                 candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2674     7926550 :                                 candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2675     7926550 :                                 candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2676     7926550 :                                 candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2677             : 
    2678     7926550 :                                 candidateArray[canIdx].drl_index = drli;
    2679             : 
    2680     7926550 :                                 candidateArray[canIdx].ref_mv_index = 0;
    2681     7926550 :                                 candidateArray[canIdx].pred_mv_weight = 0;
    2682     7926550 :                                 candidateArray[canIdx].ref_frame_type = ref_pair;
    2683     7926550 :                                 candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2684     7926550 :                                 candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2685             : 
    2686     7926550 :                                 candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2687     7926550 :                                 candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2688             : 
    2689     7926550 :                                 candidateArray[canIdx].motion_vector_pred_x[REF_LIST_0] = ref_mv[0].as_mv.col;
    2690     7926550 :                                 candidateArray[canIdx].motion_vector_pred_y[REF_LIST_0] = ref_mv[0].as_mv.row;
    2691             : 
    2692     7926550 :                                 context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2693     7926550 :                                 context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2694     7926550 :                                 context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2695     7926550 :                                 context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2696     7926550 :                                 context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2697     7926550 :                                 ++context_ptr->injected_mv_count_bipred;
    2698             : 
    2699             :                                 //NEW_NEARMV
    2700     7926550 :                                 determine_compound_mode(
    2701             :                                     picture_control_set_ptr,
    2702             :                                     context_ptr,
    2703     7926550 :                                     &candidateArray[canIdx],
    2704             :                                     cur_type);
    2705             : 
    2706     7926240 :                                 INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2707             :                         }
    2708             :                     }
    2709             :                 }
    2710             :             }
    2711             :             //NEAR_NEWMV
    2712             :             {
    2713     1488620 :                maxDrlIndex = GetMaxDrlIndex(xd->ref_mv_count[ref_pair], NEAR_NEWMV);
    2714             : 
    2715     3823830 :                for (drli = 0; drli < maxDrlIndex; drli++) {
    2716     2335250 :                    get_av1_mv_pred_drl(
    2717             :                        context_ptr,
    2718             :                        context_ptr->cu_ptr,
    2719             :                        ref_pair,
    2720             :                        1,
    2721             :                        NEAR_NEWMV,
    2722             :                        drli,
    2723             :                        nearestmv,
    2724             :                        nearmv,
    2725             :                        ref_mv);
    2726             : 
    2727             :                    //NEAR_NEWMV
    2728     2335290 :                    const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[context_ptr->me_sb_addr];
    2729             : 
    2730     2335290 :                    int16_t to_inject_mv_x_l0 = nearmv[0].as_mv.col;
    2731     2335290 :                    int16_t to_inject_mv_y_l0 = nearmv[0].as_mv.row;
    2732     2335290 :                    int16_t to_inject_mv_x_l1 = me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (get_list_idx(rf[1]) << 2) : (get_list_idx(rf[1]) << 1)) + ref_idx_1].x_mv << 1;//context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.col;
    2733     2335310 :                    int16_t to_inject_mv_y_l1 = me_results->me_mv_array[context_ptr->me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (get_list_idx(rf[1]) << 2) : (get_list_idx(rf[1]) << 1)) + ref_idx_1].y_mv << 1;//context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[ref_pair][0].comp_mv.as_mv.row;
    2734             : 
    2735     2335260 :                    inj_mv = context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, ref_pair) == EB_FALSE;
    2736             : 
    2737     2335310 :                    if (inj_mv) {
    2738             : 
    2739     2214870 :                        context_ptr->variance_ready = 0 ;
    2740    10101500 :                        for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types ; cur_type++){
    2741             :                            // If two predictors are very similar, skip wedge compound mode search
    2742     7886930 :                            if (context_ptr->variance_ready)
    2743           0 :                                if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEAR_NEWMV) && context_ptr->prediction_mse  < 64))
    2744           0 :                                    continue;
    2745             : 
    2746     7886930 :                            candidateArray[canIdx].type = INTER_MODE;
    2747     7886930 :                            candidateArray[canIdx].inter_mode = NEAR_NEWMV;
    2748     7886930 :                            candidateArray[canIdx].pred_mode = NEAR_NEWMV;
    2749     7886930 :                            candidateArray[canIdx].motion_mode = SIMPLE_TRANSLATION;
    2750     7886930 :                            candidateArray[canIdx].is_compound = 1;
    2751     7886930 :                            candidateArray[canIdx].is_interintra_used = 0;
    2752             : 
    2753     7886930 :                            candidateArray[canIdx].distortion_ready = 0;
    2754     7886930 :                            candidateArray[canIdx].use_intrabc = 0;
    2755     7886930 :                            candidateArray[canIdx].merge_flag = EB_FALSE;
    2756             : 
    2757     7886930 :                            candidateArray[canIdx].prediction_direction[0] = BI_PRED;
    2758     7886930 :                            candidateArray[canIdx].is_new_mv = 0;
    2759     7886930 :                            candidateArray[canIdx].is_zero_mv = 0;
    2760     7886930 :                            candidateArray[canIdx].motion_vector_xl0 = to_inject_mv_x_l0;
    2761     7886930 :                            candidateArray[canIdx].motion_vector_yl0 = to_inject_mv_y_l0;
    2762     7886930 :                            candidateArray[canIdx].motion_vector_xl1 = to_inject_mv_x_l1;
    2763     7886930 :                            candidateArray[canIdx].motion_vector_yl1 = to_inject_mv_y_l1;
    2764     7886930 :                            candidateArray[canIdx].drl_index = drli;
    2765     7886930 :                            candidateArray[canIdx].ref_mv_index = 0;
    2766     7886930 :                            candidateArray[canIdx].pred_mv_weight = 0;
    2767     7886930 :                            candidateArray[canIdx].ref_frame_type = ref_pair;
    2768     7886930 :                            candidateArray[canIdx].ref_frame_index_l0 = ref_idx_0;
    2769     7886930 :                            candidateArray[canIdx].ref_frame_index_l1 = ref_idx_1;
    2770             : 
    2771     7886930 :                            candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2772     7886930 :                            candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2773             : 
    2774     7886930 :                            candidateArray[canIdx].motion_vector_pred_x[REF_LIST_1] = ref_mv[1].as_mv.col;
    2775     7886930 :                            candidateArray[canIdx].motion_vector_pred_y[REF_LIST_1] = ref_mv[1].as_mv.row;
    2776             : 
    2777     7886930 :                            context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    2778     7886930 :                            context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    2779     7886930 :                            context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    2780     7886930 :                            context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    2781     7886930 :                            context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = ref_pair;
    2782     7886930 :                            ++context_ptr->injected_mv_count_bipred;
    2783             : 
    2784             :                            //NEAR_NEWMV
    2785     7886930 :                            determine_compound_mode(
    2786             :                                picture_control_set_ptr,
    2787             :                                context_ptr,
    2788     7886930 :                                &candidateArray[canIdx],
    2789             :                                cur_type);
    2790             : 
    2791     7886610 :                            INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2792             :                         }
    2793             :                    }
    2794             :                }
    2795             :             }
    2796             : #endif
    2797             :         }
    2798             :     }
    2799             : 
    2800             :     //update tot Candidate count
    2801     2544010 :     *candTotCnt = canIdx;
    2802     2544010 : }
    2803             : 
    2804      264850 : void inject_warped_motion_candidates(
    2805             :     PictureControlSet              *picture_control_set_ptr,
    2806             :     struct ModeDecisionContext     *context_ptr,
    2807             :     CodingUnit                     *cu_ptr,
    2808             :     uint32_t                       *candTotCnt,
    2809             :     SsMeContext                    *ss_mecontext,
    2810             :     MeLcuResults                   *meResult,
    2811             :     EbBool                          use_close_loop_me,
    2812             :     uint32_t                        close_loop_me_index)
    2813             : {
    2814      264850 :     uint32_t canIdx = *candTotCnt;
    2815      264850 :     ModeDecisionCandidate *candidateArray = context_ptr->fast_candidate_array;
    2816      264850 :     MacroBlockD  *xd = cu_ptr->av1xd;
    2817             :     uint8_t drli, maxDrlIndex;
    2818             :     IntMv nearestmv[2], nearmv[2], ref_mv[2];
    2819             : 
    2820      264850 :     int inside_tile = 1;
    2821      264850 :     SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet *)picture_control_set_ptr->parent_pcs_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    2822      264850 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    2823      264850 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    2824      264850 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    2825             : 
    2826      264850 :     if(umv0tile)
    2827           0 :         inside_tile = is_inside_tile_boundary(&(xd->tile), context_ptr->cu_ptr->ref_mvs[LAST_FRAME][0].as_mv.col, context_ptr->cu_ptr->ref_mvs[LAST_FRAME][0].as_mv.row, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2828      264850 :     if(inside_tile)
    2829             :     {
    2830             :     //NEAREST_L0
    2831      264851 :         candidateArray[canIdx].type = INTER_MODE;
    2832      264851 :         candidateArray[canIdx].inter_mode = NEARESTMV;
    2833      264851 :         candidateArray[canIdx].pred_mode = NEARESTMV;
    2834      264851 :         candidateArray[canIdx].motion_mode = WARPED_CAUSAL;
    2835      264851 :         candidateArray[canIdx].wm_params_l0.wmtype = AFFINE;
    2836      264851 :         candidateArray[canIdx].is_compound = 0;
    2837             : #if II_COMP_FLAG
    2838      264851 :         candidateArray[canIdx].is_interintra_used = 0;
    2839             : #endif
    2840      264851 :         candidateArray[canIdx].distortion_ready = 0;
    2841      264851 :         candidateArray[canIdx].use_intrabc = 0;
    2842      264851 :         candidateArray[canIdx].merge_flag = EB_FALSE;
    2843      264851 :         candidateArray[canIdx].prediction_direction[0] = UNI_PRED_LIST_0;
    2844      264851 :         candidateArray[canIdx].is_new_mv = 0;
    2845      264851 :         candidateArray[canIdx].is_zero_mv = 0;
    2846      264851 :         candidateArray[canIdx].motion_vector_xl0 = context_ptr->cu_ptr->ref_mvs[LAST_FRAME][0].as_mv.col;
    2847      264851 :         candidateArray[canIdx].motion_vector_yl0 = context_ptr->cu_ptr->ref_mvs[LAST_FRAME][0].as_mv.row;
    2848      264851 :         candidateArray[canIdx].drl_index = 0;
    2849      264851 :         candidateArray[canIdx].ref_mv_index = 0;
    2850      264851 :         candidateArray[canIdx].pred_mv_weight = 0;
    2851      264851 :         candidateArray[canIdx].ref_frame_type = LAST_FRAME;
    2852      264851 :         candidateArray[canIdx].ref_frame_index_l0 = 0;
    2853      264851 :         candidateArray[canIdx].ref_frame_index_l1 = -1;
    2854      264851 :         candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2855      264851 :         candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2856             : 
    2857             :         Mv mv_0;
    2858      264851 :         mv_0.x = candidateArray[canIdx].motion_vector_xl0;
    2859      264851 :         mv_0.y = candidateArray[canIdx].motion_vector_yl0;
    2860             :         MvUnit mv_unit;
    2861      264851 :         mv_unit.mv[0] = mv_0;
    2862      529701 :         candidateArray[canIdx].local_warp_valid = warped_motion_parameters(
    2863             :             picture_control_set_ptr,
    2864             :             context_ptr->cu_ptr,
    2865             :             &mv_unit,
    2866             :             context_ptr->blk_geom,
    2867      264851 :             context_ptr->cu_origin_x,
    2868      264851 :             context_ptr->cu_origin_y,
    2869      264851 :             candidateArray[canIdx].ref_frame_type,
    2870      264851 :             &candidateArray[canIdx].wm_params_l0,
    2871      264851 :             &candidateArray[canIdx].num_proj_ref);
    2872             : 
    2873      264850 :         if (candidateArray[canIdx].local_warp_valid)
    2874       60959 :             INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2875             :     }
    2876             : 
    2877             :     //NEAR_L0
    2878      264849 :     maxDrlIndex = GetMaxDrlIndex(xd->ref_mv_count[LAST_FRAME], NEARMV);
    2879      843052 :     for (drli = 0; drli < maxDrlIndex; drli++) {
    2880      578203 :         get_av1_mv_pred_drl(
    2881             :             context_ptr,
    2882             :             cu_ptr,
    2883             :             LAST_FRAME,
    2884             :             0,
    2885             :             NEARMV,
    2886             :             drli,
    2887             :             nearestmv,
    2888             :             nearmv,
    2889             :             ref_mv);
    2890      578206 :             candidateArray[canIdx].type = INTER_MODE;
    2891      578206 :             candidateArray[canIdx].inter_mode = NEARMV;
    2892      578206 :             candidateArray[canIdx].pred_mode = NEARMV;
    2893      578206 :             candidateArray[canIdx].motion_mode = WARPED_CAUSAL;
    2894      578206 :             candidateArray[canIdx].wm_params_l0.wmtype = AFFINE;
    2895      578206 :             candidateArray[canIdx].is_compound = 0;
    2896             : #if II_COMP_FLAG
    2897      578206 :             candidateArray[canIdx].is_interintra_used = 0;
    2898             : #endif
    2899      578206 :             candidateArray[canIdx].distortion_ready = 0;
    2900      578206 :             candidateArray[canIdx].use_intrabc = 0;
    2901      578206 :             candidateArray[canIdx].merge_flag = EB_FALSE;
    2902      578206 :             candidateArray[canIdx].prediction_direction[0] = UNI_PRED_LIST_0;
    2903      578206 :             candidateArray[canIdx].is_new_mv = 0;
    2904      578206 :             candidateArray[canIdx].is_zero_mv = 0;
    2905      578206 :             candidateArray[canIdx].motion_vector_xl0 = nearmv[0].as_mv.col;
    2906      578206 :             candidateArray[canIdx].motion_vector_yl0 = nearmv[0].as_mv.row;
    2907      578206 :             candidateArray[canIdx].drl_index = drli;
    2908      578206 :             candidateArray[canIdx].ref_mv_index = 0;
    2909      578206 :             candidateArray[canIdx].pred_mv_weight = 0;
    2910      578206 :             candidateArray[canIdx].ref_frame_type = LAST_FRAME;
    2911      578206 :             candidateArray[canIdx].ref_frame_index_l0 = 0;
    2912      578206 :             candidateArray[canIdx].ref_frame_index_l1 = -1;
    2913      578206 :             candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2914      578206 :             candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2915             : 
    2916             :             Mv mv_0;
    2917      578206 :             mv_0.x = candidateArray[canIdx].motion_vector_xl0;
    2918      578206 :             mv_0.y = candidateArray[canIdx].motion_vector_yl0;
    2919             :             MvUnit mv_unit;
    2920      578206 :             mv_unit.mv[0] = mv_0;
    2921      578206 :             if(umv0tile)
    2922           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), mv_0.x, mv_0.y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    2923      578206 :             if(inside_tile)
    2924             :             {
    2925     1156420 :             candidateArray[canIdx].local_warp_valid = warped_motion_parameters(
    2926             :                 picture_control_set_ptr,
    2927             :                 context_ptr->cu_ptr,
    2928             :                 &mv_unit,
    2929             :                 context_ptr->blk_geom,
    2930      578219 :                 context_ptr->cu_origin_x,
    2931      578219 :                 context_ptr->cu_origin_y,
    2932      578219 :                 candidateArray[canIdx].ref_frame_type,
    2933      578219 :                 &candidateArray[canIdx].wm_params_l0,
    2934      578219 :                 &candidateArray[canIdx].num_proj_ref);
    2935             : 
    2936      578202 :             if (candidateArray[canIdx].local_warp_valid)
    2937      137373 :                 INCRMENT_CAND_TOTAL_COUNT(canIdx);
    2938             :             }
    2939             :     }
    2940             : 
    2941             :     // NEWMV L0
    2942      264849 :     const MV neighbors[9] = { { 0, 0 },
    2943             :         { 0, -2 }, { 2, 0 }, { 0, 2 }, { -2, 0 } ,
    2944             :         { 2, -2 }, { 2, 2 }, { 2, 2 }, { -2, 2 } };
    2945             : 
    2946      264849 :     IntMv  bestPredmv[2] = { {0}, {0} };
    2947             : 
    2948      264849 :     uint8_t total_me_cnt = meResult->total_me_candidate_index[context_ptr->me_block_offset];
    2949      264849 :     const MeCandidate *me_block_results = meResult->me_candidate[context_ptr->me_block_offset];
    2950             :     //const MeLcuResults_t *meResults = pictureControlSetPtr->ParentPcsPtr->meResultsPtr[lcuAddr];
    2951     2786320 :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index)
    2952             :     {
    2953     2521410 :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    2954     2521410 :         const uint8_t inter_direction = me_block_results_ptr->direction;
    2955     2521410 :         const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
    2956     2521410 :         if (inter_direction == 0) {
    2957     6468350 :     for (int i=0; i<9; i++){
    2958             : 
    2959     5821340 :         candidateArray[canIdx].type = INTER_MODE;
    2960     5821340 :         candidateArray[canIdx].distortion_ready = 0;
    2961     5821340 :         candidateArray[canIdx].use_intrabc = 0;
    2962     5821340 :         candidateArray[canIdx].merge_flag = EB_FALSE;
    2963     5821340 :         candidateArray[canIdx].prediction_direction[0] = (EbPredDirection)0;
    2964     5821340 :         candidateArray[canIdx].inter_mode = NEWMV;
    2965     5821340 :         candidateArray[canIdx].pred_mode = NEWMV;
    2966     5821340 :         candidateArray[canIdx].motion_mode = WARPED_CAUSAL;
    2967     5821340 :         candidateArray[canIdx].wm_params_l0.wmtype = AFFINE;
    2968             : 
    2969     5821340 :         candidateArray[canIdx].is_compound = 0;
    2970             : #if II_COMP_FLAG
    2971     5821340 :         candidateArray[canIdx].is_interintra_used = 0;
    2972             : #endif
    2973     5821340 :         candidateArray[canIdx].is_new_mv = 1;
    2974     5821340 :         candidateArray[canIdx].is_zero_mv = 0;
    2975             : 
    2976     5821340 :         candidateArray[canIdx].drl_index = 0;
    2977             : 
    2978             :         // Set the MV to ME result
    2979     5821340 :         candidateArray[canIdx].motion_vector_xl0 = use_close_loop_me ? ss_mecontext->inloop_me_mv[0][0][close_loop_me_index][0] << 1 : meResult->me_mv_array[context_ptr->me_block_offset][list0_ref_index].x_mv << 1;
    2980     5821340 :         candidateArray[canIdx].motion_vector_yl0 = use_close_loop_me ? ss_mecontext->inloop_me_mv[0][0][close_loop_me_index][1] << 1 : meResult->me_mv_array[context_ptr->me_block_offset][list0_ref_index].y_mv << 1;
    2981     5821340 :         candidateArray[canIdx].motion_vector_xl0 += neighbors[i].col;
    2982     5821340 :         candidateArray[canIdx].motion_vector_yl0 += neighbors[i].row;
    2983     5821340 :         candidateArray[canIdx].ref_mv_index = 0;
    2984     5821340 :         candidateArray[canIdx].pred_mv_weight = 0;
    2985     5821340 :         candidateArray[canIdx].ref_frame_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    2986     5821500 :         candidateArray[canIdx].ref_frame_index_l0 = list0_ref_index;
    2987     5821500 :         candidateArray[canIdx].ref_frame_index_l1 = -1;
    2988             : 
    2989     5821500 :         candidateArray[canIdx].transform_type[0] = DCT_DCT;
    2990     5821500 :         candidateArray[canIdx].transform_type_uv = DCT_DCT;
    2991             : 
    2992     5821500 :         ChooseBestAv1MvPred(
    2993             :             context_ptr,
    2994     5821500 :             candidateArray[canIdx].md_rate_estimation_ptr,
    2995             :             context_ptr->cu_ptr,
    2996     5821500 :             candidateArray[canIdx].ref_frame_type,
    2997     5821500 :             candidateArray[canIdx].is_compound,
    2998     5821500 :             candidateArray[canIdx].pred_mode,
    2999     5821500 :             candidateArray[canIdx].motion_vector_xl0,
    3000     5821500 :             candidateArray[canIdx].motion_vector_yl0,
    3001             :             0, 0,
    3002     5821500 :             &candidateArray[canIdx].drl_index,
    3003             :             bestPredmv);
    3004             : 
    3005     5821210 :         candidateArray[canIdx].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    3006     5821210 :         candidateArray[canIdx].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    3007             : 
    3008             :         Mv mv_0;
    3009     5821210 :         mv_0.x = candidateArray[canIdx].motion_vector_xl0;
    3010     5821210 :         mv_0.y = candidateArray[canIdx].motion_vector_yl0;
    3011             :         MvUnit mv_unit;
    3012     5821210 :         mv_unit.mv[0] = mv_0;
    3013     5821210 :         if(umv0tile)
    3014           0 :             inside_tile = is_inside_tile_boundary(&(xd->tile), mv_0.x, mv_0.y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    3015     5821210 :         if(inside_tile)
    3016             :         {
    3017    11643900 :         candidateArray[canIdx].local_warp_valid = warped_motion_parameters(
    3018             :             picture_control_set_ptr,
    3019             :             context_ptr->cu_ptr,
    3020             :             &mv_unit,
    3021             :             context_ptr->blk_geom,
    3022     5822430 :             context_ptr->cu_origin_x,
    3023     5822430 :             context_ptr->cu_origin_y,
    3024     5822430 :             candidateArray[canIdx].ref_frame_type,
    3025     5822430 :             &candidateArray[canIdx].wm_params_l0,
    3026     5822430 :             &candidateArray[canIdx].num_proj_ref);
    3027             : 
    3028     5821430 :         if (candidateArray[canIdx].local_warp_valid)
    3029      487697 :             INCRMENT_CAND_TOTAL_COUNT(canIdx);
    3030             :         }
    3031             :     }
    3032             :         }
    3033             :     }
    3034             : 
    3035      264918 :     *candTotCnt = canIdx;
    3036      264918 : }
    3037             : 
    3038             : 
    3039             : 
    3040             : #if OBMC_FLAG
    3041             : 
    3042     1781140 : static INLINE void setup_pred_plane(struct Buf2D *dst, BlockSize bsize,
    3043             :     uint8_t *src, int width, int height,
    3044             :     int stride, int mi_row, int mi_col,
    3045             :     int subsampling_x, int subsampling_y) {
    3046             :     // Offset the buffer pointer
    3047     1781140 :     if (subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
    3048           0 :         mi_row -= 1;
    3049     1781140 :     if (subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
    3050           0 :         mi_col -= 1;
    3051             : 
    3052     1781140 :     const int x = (MI_SIZE * mi_col) >> subsampling_x;
    3053     1781140 :     const int y = (MI_SIZE * mi_row) >> subsampling_y;
    3054     1781140 :     dst->buf = src + (y * stride + x);// scaled_buffer_offset(x, y, stride, scale);
    3055     1781140 :     dst->buf0 = src;
    3056     1781140 :     dst->width = width;
    3057     1781140 :     dst->height = height;
    3058     1781140 :     dst->stride = stride;
    3059     1781140 : }
    3060     1781120 : void eb_av1_setup_pred_block(BlockSize sb_type,
    3061             :     struct Buf2D dst[MAX_MB_PLANE],
    3062             :     const Yv12BufferConfig *src, int mi_row, int mi_col) {
    3063             :     int i;
    3064             : 
    3065     1781120 :     dst[0].buf = src->y_buffer;
    3066     1781120 :     dst[0].stride = src->y_stride;
    3067     1781120 :     dst[1].buf = src->u_buffer;
    3068     1781120 :     dst[2].buf = src->v_buffer;
    3069     1781120 :     dst[1].stride = dst[2].stride = src->uv_stride;
    3070             : 
    3071     1781120 :     i = 0;
    3072     1781120 :     setup_pred_plane(dst + i, sb_type, dst[i].buf,
    3073             :         i ? src->uv_crop_width : src->y_crop_width,
    3074             :         i ? src->uv_crop_height : src->y_crop_height,
    3075     1781120 :         dst[i].stride, mi_row, mi_col,
    3076             :         0, 0);
    3077     1781160 : }
    3078             : 
    3079             : // Values are now correlated to quantizer.
    3080             : static int sad_per_bit16lut_8[QINDEX_RANGE];
    3081             : static int sad_per_bit4lut_8[QINDEX_RANGE];
    3082             : 
    3083             : extern aom_variance_fn_ptr_t mefn_ptr[BlockSizeS_ALL];
    3084             : 
    3085             : int av1_find_best_obmc_sub_pixel_tree_up(
    3086             :     ModeDecisionContext *context_ptr,IntraBcContext *x, const AV1_COMMON *const cm, int mi_row, int mi_col,
    3087             :     MV *bestmv, const MV *ref_mv, int allow_hp, int error_per_bit,
    3088             :     const aom_variance_fn_ptr_t *vfp, int forced_stop, int iters_per_step,
    3089             :     int *mvjcost, int *mvcost[2], int *distortion, unsigned int *sse1,
    3090             :     int is_second, int use_accurate_subpel_search)  ;
    3091             : 
    3092             : int av1_obmc_full_pixel_search(
    3093             :     ModeDecisionContext *context_ptr,
    3094             :     IntraBcContext *x,
    3095             :     MV *mvp_full,
    3096             :     int sadpb,
    3097             :     const aom_variance_fn_ptr_t *fn_ptr,
    3098             :     const MV *ref_mv,
    3099             :     MV *dst_mv,
    3100             :     int is_second);
    3101             : 
    3102     1781200 : static void single_motion_search(
    3103             :     PictureControlSet *pcs,
    3104             :     ModeDecisionContext *context_ptr,
    3105             :     ModeDecisionCandidate        *candidate_ptr,
    3106             :     MvReferenceFrame *rf,
    3107             :     IntMv  bestPredmv,
    3108             :     IntraBcContext  *x,
    3109             :     BlockSize bsize,
    3110             :     MV*      ref_mv,
    3111             :     int ref_idx,
    3112             :     int *rate_mv) {
    3113             : 
    3114             :     (void)ref_idx;
    3115     1781200 :     const Av1Common *const cm = pcs->parent_pcs_ptr->av1_cm;
    3116     1781200 :     FrameHeader *frm_hdr = &pcs->parent_pcs_ptr->frm_hdr;
    3117             : 
    3118     1781200 :     x->xd = context_ptr->cu_ptr->av1xd;
    3119     1781200 :     const int mi_row = -x->xd->mb_to_top_edge / (8 * MI_SIZE);
    3120     1781200 :     const int mi_col = -x->xd->mb_to_left_edge / (8 * MI_SIZE);
    3121             : 
    3122     1781200 :     x->nmv_vec_cost = context_ptr->md_rate_estimation_ptr->nmv_vec_cost;
    3123     1781200 :     x->mv_cost_stack = context_ptr->md_rate_estimation_ptr->nmvcoststack;
    3124             :     // Set up limit values for MV components.
    3125             :     // Mv beyond the range do not produce new/different prediction block.
    3126     1781200 :     const int mi_width = mi_size_wide[bsize];
    3127     1781200 :     const int mi_height = mi_size_high[bsize];
    3128     1781200 :     x->mv_limits.row_min =
    3129     1781200 :         -(((mi_row + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
    3130     1781200 :     x->mv_limits.col_min = -(((mi_col + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
    3131     1781200 :     x->mv_limits.row_max = (cm->mi_rows - mi_row) * MI_SIZE + AOM_INTERP_EXTEND;
    3132     1781200 :     x->mv_limits.col_max = (cm->mi_cols - mi_col) * MI_SIZE + AOM_INTERP_EXTEND;
    3133             :     //set search paramters
    3134     1781200 :     x->sadperbit16 = sad_per_bit16lut_8[frm_hdr->quantization_params.base_q_idx];
    3135     1781200 :     x->errorperbit = context_ptr->full_lambda >> RD_EPB_SHIFT;
    3136     1781200 :     x->errorperbit += (x->errorperbit == 0);
    3137             : 
    3138             : 
    3139     1781200 :     int bestsme = INT_MAX;
    3140     1781200 :     int sadpb = x->sadperbit16;
    3141             :     MV mvp_full;
    3142             : 
    3143     1781200 :     MvLimits tmp_mv_limits = x->mv_limits;
    3144             : 
    3145             :     // Note: MV limits are modified here. Always restore the original values
    3146             :     // after full-pixel motion search.
    3147     1781200 :     eb_av1_set_mv_search_range(&x->mv_limits, ref_mv);
    3148             : 
    3149     1781150 :     mvp_full = bestPredmv.as_mv; // mbmi->mv[0].as_mv;
    3150             : 
    3151             : 
    3152     1781150 :     mvp_full.col >>= 3;
    3153     1781150 :     mvp_full.row >>= 3;
    3154             : 
    3155     1781150 :     x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; //D
    3156             : 
    3157     1781150 :     switch (candidate_ptr->motion_mode) {
    3158             : 
    3159     1781150 :     case OBMC_CAUSAL:
    3160     1781150 :         bestsme = av1_obmc_full_pixel_search(
    3161             :             context_ptr,
    3162             :             x,
    3163             :             &mvp_full,
    3164             :             sadpb,
    3165     1781150 :             &mefn_ptr[bsize],
    3166             :             ref_mv,
    3167             :             &(x->best_mv.as_mv),
    3168             :             0);
    3169     1781150 :         break;
    3170           0 :     default: assert(0 && "Invalid motion mode!\n");
    3171             :     }
    3172             : 
    3173     1781150 :     x->mv_limits = tmp_mv_limits;
    3174             : 
    3175     1781150 :     const int use_fractional_mv =
    3176     1781150 :         bestsme < INT_MAX && frm_hdr->force_integer_mv == 0;
    3177     1781150 :     if (use_fractional_mv) {
    3178             :         int dis; /* TODO: use dis in distortion calculation later. */
    3179     1781150 :         switch (candidate_ptr->motion_mode) {
    3180     1781150 :         case OBMC_CAUSAL:
    3181     1781150 :             av1_find_best_obmc_sub_pixel_tree_up(
    3182             :                 context_ptr,
    3183             :                 x,
    3184             :                 cm,
    3185             :                 mi_row,
    3186             :                 mi_col,
    3187             :                 &x->best_mv.as_mv,
    3188             :                 ref_mv,
    3189     1781150 :                 frm_hdr->allow_high_precision_mv,
    3190             :                 x->errorperbit,
    3191     1781150 :                 &mefn_ptr[bsize],
    3192             :                 0, // mv.subpel_force_stop
    3193             :                 2, //  mv.subpel_iters_per_step
    3194             :                 x->nmv_vec_cost,
    3195             :                 x->mv_cost_stack,
    3196             :                 &dis,
    3197     1781150 :                 &context_ptr->pred_sse[rf[0]],
    3198             :                 0,
    3199             :                 USE_8_TAPS );
    3200     1781140 :             break;
    3201           0 :         default: assert(0 && "Invalid motion mode!\n");
    3202             :         }
    3203             :     }
    3204     3562260 :     *rate_mv = eb_av1_mv_bit_cost(&x->best_mv.as_mv, ref_mv, x->nmv_vec_cost,
    3205     1781140 :         x->mv_cost_stack, MV_COST_WEIGHT);
    3206             : 
    3207     1781120 : }
    3208             : 
    3209     1781100 : void obmc_motion_refinement(
    3210             :     PictureControlSet          *picture_control_set_ptr,
    3211             :     struct ModeDecisionContext *context_ptr,
    3212             :     ModeDecisionCandidate    *candidate,
    3213             :     uint8_t                   ref_list_idx)
    3214             : {
    3215     1781100 :     IntMv  bestPredmv[2] = { {0}, {0} };
    3216             :     IntraBcContext  x_st;
    3217     1781100 :     IntraBcContext  *x = &x_st;
    3218             : 
    3219             :     MacroBlockD * xd;
    3220     1781100 :     xd = x->xd = context_ptr->cu_ptr->av1xd;
    3221     1781100 :     const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE);
    3222     1781100 :     const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE);
    3223             : 
    3224             :     {
    3225     1781100 :         uint8_t ref_idx = get_ref_frame_idx(candidate->ref_frame_type);
    3226     1781130 :         uint8_t list_idx = get_list_idx(candidate->ref_frame_type);
    3227     1781130 :         EbPictureBufferDesc  *reference_picture = ((EbReferenceObject*)picture_control_set_ptr->ref_pic_ptr_array[list_idx][ref_idx]->object_ptr)->reference_picture;
    3228             :         Yv12BufferConfig ref_buf;
    3229     1781130 :         link_Eb_to_aom_buffer_desc_8bit(
    3230             :             reference_picture,
    3231             :             &ref_buf);
    3232             : 
    3233             :         struct Buf2D yv12_mb[MAX_MB_PLANE];
    3234     1781120 :         eb_av1_setup_pred_block(context_ptr->blk_geom->bsize, yv12_mb, &ref_buf, mi_row, mi_col);
    3235     3562400 :         for (int i = 0; i < 1; ++i)
    3236     1781200 :             x->xdplane[i].pre[0] = yv12_mb[i];  //ref in ME
    3237             : 
    3238     1781200 :         x->plane[0].src.buf = 0;// x->xdplane[0].pre[0];
    3239     1781200 :         x->plane[0].src.buf0 = 0;
    3240             :     }
    3241             : 
    3242             :     IntMv  best_mv ;
    3243     1781200 :     best_mv.as_int = 0;
    3244     1781200 :     if (ref_list_idx == 0) {
    3245     1078320 :         best_mv.as_mv.col = candidate->motion_vector_xl0;// to_inject_mv_x;
    3246     1078320 :         best_mv.as_mv.row = candidate->motion_vector_yl0; //to_inject_mv_y;
    3247             :     }
    3248             :     else
    3249             :     {
    3250      702879 :         best_mv.as_mv.col = candidate->motion_vector_xl1;// to_inject_mv_x;
    3251      702879 :         best_mv.as_mv.row = candidate->motion_vector_yl1; //to_inject_mv_y;
    3252             :     }
    3253             :     int tmp_rate_mv;
    3254             : 
    3255             :     MvReferenceFrame rf[2];
    3256     1781200 :     rf[0] = candidate->ref_frame_type;
    3257     1781200 :     rf[1] = -1;
    3258             : 
    3259             :     MV      ref_mv;
    3260     1781200 :     ref_mv.col = candidate->motion_vector_pred_x[ref_list_idx];
    3261     1781200 :     ref_mv.row = candidate->motion_vector_pred_y[ref_list_idx];
    3262             : 
    3263     1781200 :     single_motion_search(
    3264             :         picture_control_set_ptr,
    3265             :         context_ptr,
    3266             :         candidate,
    3267             :         rf,
    3268             :         best_mv,
    3269             :         x,
    3270     1781200 :         context_ptr->blk_geom->bsize,
    3271             :         &ref_mv,
    3272             :         0,
    3273             :         &tmp_rate_mv);
    3274             : 
    3275     1781120 :     if (ref_list_idx == 0) {
    3276     1078290 :         candidate->motion_vector_xl0 = x->best_mv.as_mv.col;
    3277     1078290 :         candidate->motion_vector_yl0 = x->best_mv.as_mv.row;
    3278             :     }
    3279             :     else {
    3280      702831 :         candidate->motion_vector_xl1 = x->best_mv.as_mv.col;
    3281      702831 :         candidate->motion_vector_yl1 = x->best_mv.as_mv.row;
    3282             :     }
    3283             : 
    3284     3562240 :     ChooseBestAv1MvPred(
    3285             :         context_ptr,
    3286     1781120 :         candidate->md_rate_estimation_ptr,
    3287             :         context_ptr->cu_ptr,
    3288     1781120 :         candidate->ref_frame_type,
    3289     1781120 :         candidate->is_compound,
    3290     1781120 :         candidate->pred_mode,
    3291     1781120 :         ref_list_idx == 0 ? candidate->motion_vector_xl0 : candidate->motion_vector_xl1,
    3292     1781120 :         ref_list_idx == 0 ? candidate->motion_vector_yl0 : candidate->motion_vector_yl1,
    3293             :         0, 0,
    3294             :         &candidate->drl_index,
    3295             :         bestPredmv);
    3296             : 
    3297     1781110 :     if (ref_list_idx == 0) {
    3298     1078290 :         candidate->motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    3299     1078290 :         candidate->motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    3300             :     }
    3301             :     else {
    3302      702821 :         candidate->motion_vector_pred_x[REF_LIST_1] = bestPredmv[0].as_mv.col;
    3303      702821 :         candidate->motion_vector_pred_y[REF_LIST_1] = bestPredmv[0].as_mv.row;
    3304             :     }
    3305     1781110 : }
    3306             : #endif
    3307             : 
    3308      277744 : void inject_new_candidates(
    3309             :     const SequenceControlSet   *sequence_control_set_ptr,
    3310             :     struct ModeDecisionContext *context_ptr,
    3311             :     PictureControlSet          *picture_control_set_ptr,
    3312             :     EbBool                      isCompoundEnabled,
    3313             :     EbBool                      allow_bipred,
    3314             :     uint32_t                    me_sb_addr,
    3315             :     SsMeContext                *inloop_me_context,
    3316             :     EbBool                      use_close_loop_me,
    3317             :     uint32_t                    close_loop_me_index,
    3318             :     uint32_t                    me_block_offset,
    3319             :     uint32_t                   *candidateTotalCnt) {
    3320             : 
    3321      277744 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    3322      277744 :     IntMv  bestPredmv[2] = { {0}, {0} };
    3323      277744 :     uint32_t canTotalCnt = (*candidateTotalCnt);
    3324             : 
    3325      277744 :     const MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[me_sb_addr];
    3326      277744 :     uint8_t total_me_cnt = me_results->total_me_candidate_index[me_block_offset];
    3327      277744 :     const MeCandidate *me_block_results = me_results->me_candidate[me_block_offset];
    3328      277744 :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    3329      277744 :     int inside_tile = 1;
    3330      277744 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    3331      277744 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    3332      277744 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    3333             : #if FIX_COMPOUND
    3334      277744 :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    3335      277744 :     MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    3336             :     MD_COMP_TYPE cur_type; //NN
    3337      484042 :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    3338      203574 :         (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    3339             :         (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    3340        2724 :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    3341             : #else
    3342             :     MD_COMP_TYPE cur_type; //NN
    3343             :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    3344             :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    3345             : #endif
    3346             : #if SPEED_OPT
    3347      277744 :     if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    3348       63964 :         tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    3349             : #endif
    3350             :  #if II_COMP_FLAG && !FIX_COMPOUND
    3351             :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    3352             : #endif
    3353     2160540 :     for (uint8_t me_candidate_index = 0; me_candidate_index < total_me_cnt; ++me_candidate_index)
    3354             :     {
    3355     1882760 :         const MeCandidate *me_block_results_ptr = &me_block_results[me_candidate_index];
    3356     1882760 :         const uint8_t inter_direction = me_block_results_ptr->direction;
    3357     1882760 :         const uint8_t list0_ref_index = me_block_results_ptr->ref_idx_l0;
    3358     1882760 :         const uint8_t list1_ref_index = me_block_results_ptr->ref_idx_l1;
    3359             : 
    3360             :         /**************
    3361             :             NEWMV L0
    3362             :         ************* */
    3363     1882760 :         if (inter_direction == 0) {
    3364             : 
    3365      522244 :             int16_t to_inject_mv_x = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[me_block_offset][list0_ref_index].x_mv << 1;
    3366      522244 :             int16_t to_inject_mv_y = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[me_block_offset][list0_ref_index].y_mv << 1;
    3367      522244 :             uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    3368      522235 :             uint8_t skip_cand = check_ref_beackout(
    3369             :                 context_ptr,
    3370             :                 to_inject_ref_type,
    3371      522235 :                 context_ptr->blk_geom->shape);
    3372             : 
    3373      522230 :             inside_tile = 1;
    3374      522230 :             if(umv0tile)
    3375           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    3376      522231 :             skip_cand = skip_cand || (!inside_tile);
    3377             : 
    3378      522231 :             if (!skip_cand && (context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE)) {
    3379             : 
    3380             : #if II_COMP_FLAG    // NEWMV L0
    3381             :              MvReferenceFrame rf[2];
    3382      493896 :              rf[0] = to_inject_ref_type;
    3383      493896 :              rf[1] = -1;
    3384             : 
    3385             :             uint8_t inter_type;
    3386      493896 :             uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    3387      493944 :             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    3388             : #if OBMC_FLAG
    3389      493944 :              uint8_t is_obmc_allowed =  obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    3390      493936 :              tot_inter_types = is_obmc_allowed && picture_control_set_ptr->parent_pcs_ptr->pic_obmc_mode <= 2 ? tot_inter_types+1 : tot_inter_types;
    3391             : #endif
    3392     1572440 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    3393             :             {
    3394             : #endif
    3395     1078460 :                 candidateArray[canTotalCnt].type = INTER_MODE;
    3396     1078460 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    3397     1078460 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    3398     1078460 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3399     1078460 :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    3400     1078460 :                 candidateArray[canTotalCnt].inter_mode = NEWMV;
    3401     1078460 :                 candidateArray[canTotalCnt].pred_mode = NEWMV;
    3402     1078460 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3403             : 
    3404     1078460 :                 candidateArray[canTotalCnt].is_compound = 0;
    3405     1078460 :                 candidateArray[canTotalCnt].is_new_mv = 1;
    3406     1078460 :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    3407             : 
    3408     1078460 :                 candidateArray[canTotalCnt].drl_index = 0;
    3409             : 
    3410             :                 // Set the MV to ME result
    3411     1078460 :                 candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x;
    3412     1078460 :                 candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y;
    3413             : 
    3414             :                 // will be needed later by the rate estimation
    3415     1078460 :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    3416     1078460 :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    3417     1078460 :                 candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_0, list0_ref_index);
    3418     1078480 :                 candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    3419     1078480 :                 candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    3420             : 
    3421     1078480 :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    3422     1078480 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    3423             : 
    3424     1078480 :                 ChooseBestAv1MvPred(
    3425             :                     context_ptr,
    3426     1078480 :                     candidateArray[canTotalCnt].md_rate_estimation_ptr,
    3427             :                     context_ptr->cu_ptr,
    3428     1078480 :                     candidateArray[canTotalCnt].ref_frame_type,
    3429     1078480 :                     candidateArray[canTotalCnt].is_compound,
    3430     1078480 :                     candidateArray[canTotalCnt].pred_mode,
    3431     1078480 :                     candidateArray[canTotalCnt].motion_vector_xl0,
    3432     1078480 :                     candidateArray[canTotalCnt].motion_vector_yl0,
    3433             :                     0, 0,
    3434     1078480 :                     &candidateArray[canTotalCnt].drl_index,
    3435             :                     bestPredmv);
    3436             : 
    3437     1078520 :                 candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    3438     1078520 :                 candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    3439             : 
    3440             : #if II_COMP_FLAG
    3441     1078520 :             if (inter_type == 0) {
    3442      493957 :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    3443      493957 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3444             :             }
    3445             :             else {
    3446      584560 :                 if (is_ii_allowed) {
    3447      548526 :                     if (inter_type == 1) {
    3448      183698 :                         inter_intra_search(
    3449             :                             picture_control_set_ptr,
    3450             :                             context_ptr,
    3451      183698 :                             &candidateArray[canTotalCnt]);
    3452      183699 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    3453      183699 :                         candidateArray[canTotalCnt].use_wedge_interintra = 1;
    3454      183699 :                         candidateArray[canTotalCnt].ii_wedge_sign = 0;
    3455             :                     }
    3456      364828 :                     else if (inter_type == 2) {
    3457      183697 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    3458      183697 :                         candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    3459      183697 :                         candidateArray[canTotalCnt].use_wedge_interintra = 0;
    3460             :                     }
    3461             :                 }
    3462             :  #if OBMC_FLAG
    3463      584561 :                 if (is_obmc_allowed && inter_type == tot_inter_types-1){
    3464      217176 :                         candidateArray[canTotalCnt].is_interintra_used = 0;
    3465      217176 :                         candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    3466             : 
    3467      217176 :                         obmc_motion_refinement(
    3468             :                             picture_control_set_ptr,
    3469             :                             context_ptr,
    3470      217176 :                             &candidateArray[canTotalCnt],
    3471             :                             REF_LIST_0);
    3472             :                     }
    3473             : #endif
    3474             :             }
    3475             : #endif
    3476     1078520 :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    3477             : 
    3478             : #if II_COMP_FLAG
    3479             :             }
    3480             : #endif
    3481      493979 :                 context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    3482      493979 :                 context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    3483      493979 :                 context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    3484      493979 :                 ++context_ptr->injected_mv_count_l0;
    3485             : 
    3486             :             }
    3487             : 
    3488             :             }
    3489             : 
    3490     1882830 :         if (isCompoundEnabled) {
    3491             :             /**************
    3492             :                NEWMV L1
    3493             :            ************* */
    3494     1841690 :             if (inter_direction == 1) {
    3495      369651 :                 int16_t to_inject_mv_x = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].x_mv << 1;
    3496      369651 :                 int16_t to_inject_mv_y = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? 4 : 2) + list1_ref_index].y_mv << 1;
    3497      369651 :                 uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    3498      369651 :                 uint8_t skip_cand = check_ref_beackout(
    3499             :                     context_ptr,
    3500             :                     to_inject_ref_type,
    3501      369651 :                     context_ptr->blk_geom->shape);
    3502             : 
    3503      369646 :                 inside_tile = 1;
    3504      369646 :                 if(umv0tile)
    3505           0 :                     inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    3506      369647 :                 skip_cand = skip_cand || !inside_tile;
    3507             : 
    3508      369647 :                 if (!skip_cand && (context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE)) {
    3509             :   #if II_COMP_FLAG // NEWMV L1
    3510             :              MvReferenceFrame rf[2];
    3511      347291 :              rf[0] = to_inject_ref_type;
    3512      347291 :              rf[1] = -1;
    3513             : 
    3514             :             uint8_t inter_type;
    3515      347291 :             uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    3516      347268 :             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    3517             : #if OBMC_FLAG
    3518      347268 :             uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    3519      347264 :             tot_inter_types = is_obmc_allowed  && picture_control_set_ptr->parent_pcs_ptr->pic_obmc_mode <= 2 ? tot_inter_types + 1 : tot_inter_types;
    3520             : #endif
    3521     1069070 :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    3522             :             {
    3523             : #endif
    3524      721785 :                     candidateArray[canTotalCnt].type = INTER_MODE;
    3525      721785 :                     candidateArray[canTotalCnt].distortion_ready = 0;
    3526      721785 :                     candidateArray[canTotalCnt].use_intrabc = 0;
    3527      721785 :                     candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3528      721785 :                     candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)1;
    3529             : 
    3530      721785 :                     candidateArray[canTotalCnt].inter_mode = NEWMV;
    3531      721785 :                     candidateArray[canTotalCnt].pred_mode = NEWMV;
    3532      721785 :                     candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3533             : 
    3534      721785 :                     candidateArray[canTotalCnt].is_compound = 0;
    3535      721785 :                     candidateArray[canTotalCnt].is_new_mv = 1;
    3536      721785 :                     candidateArray[canTotalCnt].is_zero_mv = 0;
    3537             : 
    3538      721785 :                     candidateArray[canTotalCnt].drl_index = 0;
    3539             : 
    3540             :                     // Set the MV to ME result
    3541      721785 :                     candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x;
    3542      721785 :                     candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y;
    3543             : 
    3544             :                     // will be needed later by the rate estimation
    3545      721785 :                     candidateArray[canTotalCnt].ref_mv_index = 0;
    3546      721785 :                     candidateArray[canTotalCnt].pred_mv_weight = 0;
    3547      721785 :                     candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_1, list1_ref_index);
    3548      721809 :                     candidateArray[canTotalCnt].ref_frame_index_l0 = -1;
    3549      721809 :                     candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    3550             : 
    3551      721809 :                     candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    3552      721809 :                     candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    3553             : 
    3554      721809 :                     ChooseBestAv1MvPred(
    3555             :                         context_ptr,
    3556      721809 :                         candidateArray[canTotalCnt].md_rate_estimation_ptr,
    3557             :                         context_ptr->cu_ptr,
    3558      721809 :                         candidateArray[canTotalCnt].ref_frame_type,
    3559      721809 :                         candidateArray[canTotalCnt].is_compound,
    3560      721809 :                         candidateArray[canTotalCnt].pred_mode,
    3561      721809 :                         candidateArray[canTotalCnt].motion_vector_xl1,
    3562      721809 :                         candidateArray[canTotalCnt].motion_vector_yl1,
    3563             :                         0, 0,
    3564      721809 :                         &candidateArray[canTotalCnt].drl_index,
    3565             :                         bestPredmv);
    3566             : 
    3567      721814 :                     candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[0].as_mv.col;
    3568      721814 :                     candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[0].as_mv.row;
    3569             : #if II_COMP_FLAG
    3570      721814 :             if (inter_type == 0) {
    3571      347284 :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    3572      347284 :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3573             :             }
    3574             :             else {
    3575      374530 :                 if (is_ii_allowed) {
    3576      352156 :                     if (inter_type == 1) {
    3577      118133 :                         inter_intra_search(
    3578             :                             picture_control_set_ptr,
    3579             :                             context_ptr,
    3580      118133 :                             &candidateArray[canTotalCnt]);
    3581      118133 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    3582      118133 :                         candidateArray[canTotalCnt].use_wedge_interintra = 1;
    3583      118133 :                         candidateArray[canTotalCnt].ii_wedge_sign = 0;
    3584             :                     }
    3585      234023 :                     else if (inter_type == 2) {
    3586      118133 :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    3587      118133 :                         candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    3588      118133 :                         candidateArray[canTotalCnt].use_wedge_interintra = 0;
    3589             :                     }
    3590             :                 }
    3591             : #if OBMC_FLAG
    3592      374530 :                 if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    3593      138268 :                     candidateArray[canTotalCnt].is_interintra_used = 0;
    3594      138268 :                     candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    3595             : 
    3596      138268 :                     obmc_motion_refinement(
    3597             :                         picture_control_set_ptr,
    3598             :                         context_ptr,
    3599      138268 :                         &candidateArray[canTotalCnt],
    3600             :                         REF_LIST_1);
    3601             :                 }
    3602             : #endif
    3603             : 
    3604             :             }
    3605             : #endif
    3606      721815 :                     INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    3607             : 
    3608             : #if II_COMP_FLAG
    3609             :             }
    3610             : #endif
    3611      347289 :                     context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    3612      347289 :                     context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    3613      347289 :                     context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = to_inject_ref_type;
    3614      347289 :                     ++context_ptr->injected_mv_count_l1;
    3615             :                 }
    3616             : 
    3617             :                 }
    3618             :             /**************
    3619             :                NEW_NEWMV
    3620             :             ************* */
    3621     1841690 :             if (allow_bipred) {
    3622             : 
    3623     1065080 :                 if (inter_direction == 2) {
    3624      566804 :                     int16_t to_inject_mv_x_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[me_block_offset][list0_ref_index].x_mv << 1;
    3625      566804 :                     int16_t to_inject_mv_y_l0 = use_close_loop_me ? inloop_me_context->inloop_me_mv[0][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[me_block_offset][list0_ref_index].y_mv << 1;
    3626      566804 :                     int16_t to_inject_mv_x_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][0] << 1 : me_results->me_mv_array[me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].x_mv << 1;
    3627      566804 :                     int16_t to_inject_mv_y_l1 = use_close_loop_me ? inloop_me_context->inloop_me_mv[1][0][close_loop_me_index][1] << 1 : me_results->me_mv_array[me_block_offset][((sequence_control_set_ptr->mrp_mode == 0) ? (me_block_results_ptr->ref1_list << 2) : (me_block_results_ptr->ref1_list << 1)) + list1_ref_index].y_mv << 1;
    3628             :                     MvReferenceFrame rf[2];
    3629      566804 :                     rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    3630      566814 :                     rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    3631      566821 :                     uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    3632      566809 :                     uint8_t skip_cand = check_ref_beackout(
    3633             :                         context_ptr,
    3634             :                         to_inject_ref_type,
    3635      566809 :                         context_ptr->blk_geom->shape);
    3636             : 
    3637      566796 :                     inside_tile = 1;
    3638      566796 :                     if(umv0tile) {
    3639           0 :                         inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    3640           0 :                                       is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l1, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    3641             :                     }
    3642      566796 :                     skip_cand = skip_cand || (!inside_tile);
    3643      566796 :                     if (!skip_cand && (context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE)) {
    3644      546615 :                         context_ptr->variance_ready = 0;
    3645     2385990 :                         for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    3646             :                         {
    3647     1839410 :                             if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    3648             :                             // If two predictors are very similar, skip wedge compound mode search
    3649     1839410 :                             if (context_ptr->variance_ready)
    3650           0 :                                 if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEWMV) && context_ptr->prediction_mse < 64))
    3651           0 :                                     continue;
    3652     1839410 :                         candidateArray[canTotalCnt].type = INTER_MODE;
    3653             : 
    3654     1839410 :                         candidateArray[canTotalCnt].distortion_ready = 0;
    3655     1839410 :                         candidateArray[canTotalCnt].use_intrabc = 0;
    3656             : 
    3657     1839410 :                         candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3658             : 
    3659     1839410 :                         candidateArray[canTotalCnt].is_new_mv = 1;
    3660     1839410 :                         candidateArray[canTotalCnt].is_zero_mv = 0;
    3661             : 
    3662     1839410 :                         candidateArray[canTotalCnt].drl_index = 0;
    3663             : 
    3664             :                         // Set the MV to ME result
    3665             : 
    3666     1839410 :                         candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    3667     1839410 :                         candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    3668     1839410 :                         candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    3669     1839410 :                         candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    3670             : 
    3671             :                         // will be needed later by the rate estimation
    3672     1839410 :                         candidateArray[canTotalCnt].ref_mv_index = 0;
    3673     1839410 :                         candidateArray[canTotalCnt].pred_mv_weight = 0;
    3674             : 
    3675     1839410 :                         candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    3676     1839410 :                         candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    3677     1839410 :                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3678     1839410 :                         candidateArray[canTotalCnt].is_compound = 1;
    3679             : #if II_COMP_FLAG
    3680     1839410 :                         candidateArray[canTotalCnt].is_interintra_used = 0;
    3681             : #endif
    3682     1839410 :                         candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    3683             :                         MvReferenceFrame rf[2];
    3684     1839410 :                         rf[0] = svt_get_ref_frame_type(me_block_results_ptr->ref0_list, list0_ref_index);
    3685     1839420 :                         rf[1] = svt_get_ref_frame_type(me_block_results_ptr->ref1_list, list1_ref_index);
    3686     1839410 :                         candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    3687             : 
    3688     1839400 :                         candidateArray[canTotalCnt].ref_frame_index_l0 = list0_ref_index;
    3689     1839400 :                         candidateArray[canTotalCnt].ref_frame_index_l1 = list1_ref_index;
    3690     1839400 :                         candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    3691     1839400 :                         candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    3692     1839400 :                         ChooseBestAv1MvPred(
    3693             :                             context_ptr,
    3694     1839400 :                             candidateArray[canTotalCnt].md_rate_estimation_ptr,
    3695             :                             context_ptr->cu_ptr,
    3696     1839400 :                             candidateArray[canTotalCnt].ref_frame_type,
    3697     1839400 :                             candidateArray[canTotalCnt].is_compound,
    3698     1839400 :                             candidateArray[canTotalCnt].pred_mode,
    3699     1839400 :                             candidateArray[canTotalCnt].motion_vector_xl0,
    3700     1839400 :                             candidateArray[canTotalCnt].motion_vector_yl0,
    3701     1839400 :                             candidateArray[canTotalCnt].motion_vector_xl1,
    3702     1839400 :                             candidateArray[canTotalCnt].motion_vector_yl1,
    3703     1839400 :                             &candidateArray[canTotalCnt].drl_index,
    3704             :                             bestPredmv);
    3705             : 
    3706     1839340 :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    3707     1839340 :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    3708     1839340 :                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    3709     1839340 :                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    3710             :                         //NEW_NEW
    3711     1839340 :                         determine_compound_mode(
    3712             :                             picture_control_set_ptr,
    3713             :                             context_ptr,
    3714     1839340 :                             &candidateArray[canTotalCnt],
    3715             :                             cur_type);
    3716     1839370 :                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    3717             : 
    3718     1839370 :                         context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    3719     1839370 :                         context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    3720     1839370 :                         context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    3721     1839370 :                         context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    3722     1839370 :                         context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    3723     1839370 :                         ++context_ptr->injected_mv_count_bipred;
    3724             :                         }
    3725             :                     }
    3726             : 
    3727             :                     }
    3728             :                 }
    3729             :             }
    3730             :             }
    3731             :     // update the total number of candidates injected
    3732      277782 :     (*candidateTotalCnt) = canTotalCnt;
    3733      277782 :         }
    3734      675252 :         void inject_predictive_me_candidates(
    3735             :             //const SequenceControlSet   *sequence_control_set_ptr,
    3736             :             struct ModeDecisionContext *context_ptr,
    3737             :             PictureControlSet          *picture_control_set_ptr,
    3738             :             EbBool                      isCompoundEnabled,
    3739             :             EbBool                      allow_bipred,
    3740             :             uint32_t                   *candidateTotalCnt) {
    3741             : 
    3742      675252 :             ModeDecisionCandidate *candidateArray = context_ptr->fast_candidate_array;
    3743      675252 :             IntMv  bestPredmv[2] = { {0}, {0} };
    3744      675252 :             uint32_t canTotalCnt = (*candidateTotalCnt);
    3745             : 
    3746             : #if FIX_COMPOUND
    3747      675252 :             BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    3748      675252 :             MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    3749             :             MD_COMP_TYPE cur_type; //BIP 3x3 MiSize >= BLOCK_8X8 && MiSize <= BLOCK_32X32)
    3750      675252 :             MD_COMP_TYPE tot_comp_types = (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    3751             :                 (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    3752           0 :                 picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;//MD_COMP_DIST;// MD_COMP_AVG;//
    3753             : #else
    3754             :             BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    3755             :             MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    3756             :             MD_COMP_TYPE cur_type; //BIP 3x3 MiSize >= BLOCK_8X8 && MiSize <= BLOCK_32X32)
    3757             :             MD_COMP_TYPE tot_comp_types = (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    3758             :                 (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    3759             :                 picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;//MD_COMP_DIST;// MD_COMP_AVG;//
    3760             : #endif
    3761             : #if SPEED_OPT
    3762      675252 :             if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    3763      174279 :                 tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    3764             : #endif
    3765             :             uint8_t listIndex;
    3766             :             uint8_t ref_pic_index;
    3767      675252 :             listIndex = REF_LIST_0;
    3768             :             {
    3769             :                 // Ref Picture Loop
    3770     3376170 :                 for (ref_pic_index = 0; ref_pic_index < 4; ++ref_pic_index) {
    3771     2700910 :                     if (context_ptr->valid_refined_mv[listIndex][ref_pic_index]) {
    3772     1634200 :                         int16_t to_inject_mv_x = context_ptr->best_spatial_pred_mv[listIndex][ref_pic_index][0];
    3773     1634200 :                         int16_t to_inject_mv_y = context_ptr->best_spatial_pred_mv[listIndex][ref_pic_index][1];
    3774     1634200 :                         uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, ref_pic_index);
    3775     1634190 :                         if (context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE) {
    3776             : #if OBMC_FLAG
    3777             :                             MvReferenceFrame rf[2];
    3778     1469090 :                             rf[0] = to_inject_ref_type;
    3779     1469090 :                             rf[1] = -1;
    3780             :                             uint8_t inter_type;
    3781     1469090 :                             uint8_t is_ii_allowed = 0;// svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    3782     1469090 :                             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    3783     1469090 :                             uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    3784     1469080 :                             tot_inter_types = is_obmc_allowed ? tot_inter_types + 1 : tot_inter_types;
    3785     3799230 :                             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    3786             :                             {
    3787             : #endif
    3788     2330090 :                             candidateArray[canTotalCnt].type = INTER_MODE;
    3789     2330090 :                             candidateArray[canTotalCnt].distortion_ready = 0;
    3790     2330090 :                             candidateArray[canTotalCnt].use_intrabc = 0;
    3791     2330090 :                             candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3792     2330090 :                             candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    3793     2330090 :                             candidateArray[canTotalCnt].inter_mode = NEWMV;
    3794     2330090 :                             candidateArray[canTotalCnt].pred_mode = NEWMV;
    3795     2330090 :                             candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3796     2330090 :                             candidateArray[canTotalCnt].is_compound = 0;
    3797             : #if II_COMP_FLAG // PME OFF   L0
    3798     2330090 :                             candidateArray[canTotalCnt].is_interintra_used = 0;
    3799             : #endif
    3800     2330090 :                             candidateArray[canTotalCnt].is_new_mv = 1;
    3801     2330090 :                             candidateArray[canTotalCnt].is_zero_mv = 0;
    3802     2330090 :                             candidateArray[canTotalCnt].drl_index = 0;
    3803     2330090 :                             candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x;
    3804     2330090 :                             candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y;
    3805     2330090 :                             candidateArray[canTotalCnt].ref_mv_index = 0;
    3806     2330090 :                             candidateArray[canTotalCnt].pred_mv_weight = 0;
    3807     2330090 :                             candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_0, ref_pic_index);
    3808     2330160 :                             candidateArray[canTotalCnt].ref_frame_index_l0 = ref_pic_index;
    3809     2330160 :                             candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    3810     2330160 :                             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    3811     2330160 :                             candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    3812             : 
    3813     2330160 :                             ChooseBestAv1MvPred(
    3814             :                                 context_ptr,
    3815     2330160 :                                 candidateArray[canTotalCnt].md_rate_estimation_ptr,
    3816             :                                 context_ptr->cu_ptr,
    3817     2330160 :                                 candidateArray[canTotalCnt].ref_frame_type,
    3818     2330160 :                                 candidateArray[canTotalCnt].is_compound,
    3819     2330160 :                                 candidateArray[canTotalCnt].pred_mode,
    3820     2330160 :                                 candidateArray[canTotalCnt].motion_vector_xl0,
    3821     2330160 :                                 candidateArray[canTotalCnt].motion_vector_yl0,
    3822             :                                 0, 0,
    3823     2330160 :                                 &candidateArray[canTotalCnt].drl_index,
    3824             :                                 bestPredmv);
    3825             : 
    3826     2330220 :                             candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    3827     2330220 :                             candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    3828             : #if OBMC_FLAG
    3829     2330220 :                             if (inter_type == 0) {
    3830     1469110 :                                 candidateArray[canTotalCnt].is_interintra_used = 0;
    3831     1469110 :                                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3832             :                             }
    3833             :                             else {
    3834      861116 :                                 if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    3835      861116 :                                     candidateArray[canTotalCnt].is_interintra_used = 0;
    3836      861116 :                                     candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    3837             : 
    3838      861116 :                                     obmc_motion_refinement(
    3839             :                                         picture_control_set_ptr,
    3840             :                                         context_ptr,
    3841      861116 :                                         &candidateArray[canTotalCnt],
    3842             :                                         REF_LIST_0);
    3843             :                                 }
    3844             :                             }
    3845             : #endif
    3846     2330240 :                             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    3847     2330150 :                             context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    3848     2330150 :                             context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    3849     2330150 :                             context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    3850     2330150 :                             ++context_ptr->injected_mv_count_l0;
    3851             : #if OBMC_FLAG
    3852             :                         }
    3853             : #endif
    3854             :                         }
    3855             :                     }
    3856             :                 }
    3857             :             }
    3858      675261 :             if (isCompoundEnabled) {
    3859             :                 /**************
    3860             :                    NEWMV L1
    3861             :                ************* */
    3862      637579 :                 listIndex = REF_LIST_1;
    3863             :                 {
    3864             :                     // Ref Picture Loop
    3865     2550300 :                     for (ref_pic_index = 0; ref_pic_index < 3; ++ref_pic_index) {
    3866     1912680 :                         if (context_ptr->valid_refined_mv[listIndex][ref_pic_index]) {
    3867     1089630 :                             int16_t to_inject_mv_x = context_ptr->best_spatial_pred_mv[listIndex][ref_pic_index][0];
    3868     1089630 :                             int16_t to_inject_mv_y = context_ptr->best_spatial_pred_mv[listIndex][ref_pic_index][1];
    3869     1089630 :                             uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_1, ref_pic_index);
    3870     1089630 :                             if (context_ptr->injected_mv_count_l1 == 0 || mrp_is_already_injected_mv_l1(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE) {
    3871             : #if OBMC_FLAG
    3872             :                                 MvReferenceFrame rf[2];
    3873      961480 :                                 rf[0] = to_inject_ref_type;
    3874      961480 :                                 rf[1] = -1;
    3875             :                                 uint8_t inter_type;
    3876      961480 :                                 uint8_t is_ii_allowed = 0;// svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, NEWMV, rf);
    3877      961480 :                                 uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    3878      961480 :                                 uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    3879      961483 :                                 tot_inter_types = is_obmc_allowed ? tot_inter_types + 1 : tot_inter_types;
    3880     2487540 :                                 for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    3881             :                                 {
    3882             : #endif
    3883     1526040 :                                 candidateArray[canTotalCnt].type = INTER_MODE;
    3884     1526040 :                                 candidateArray[canTotalCnt].distortion_ready = 0;
    3885     1526040 :                                 candidateArray[canTotalCnt].use_intrabc = 0;
    3886     1526040 :                                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3887     1526040 :                                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)1;
    3888     1526040 :                                 candidateArray[canTotalCnt].inter_mode = NEWMV;
    3889     1526040 :                                 candidateArray[canTotalCnt].pred_mode = NEWMV;
    3890     1526040 :                                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3891     1526040 :                                 candidateArray[canTotalCnt].is_compound = 0;
    3892             : #if II_COMP_FLAG // PME OFF   L1
    3893     1526040 :                                 candidateArray[canTotalCnt].is_interintra_used = 0;
    3894             : #endif
    3895     1526040 :                                 candidateArray[canTotalCnt].is_new_mv = 1;
    3896     1526040 :                                 candidateArray[canTotalCnt].is_zero_mv = 0;
    3897     1526040 :                                 candidateArray[canTotalCnt].drl_index = 0;
    3898     1526040 :                                 candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x;
    3899     1526040 :                                 candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y;
    3900     1526040 :                                 candidateArray[canTotalCnt].ref_mv_index = 0;
    3901     1526040 :                                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    3902     1526040 :                                 candidateArray[canTotalCnt].ref_frame_type = svt_get_ref_frame_type(REF_LIST_1, ref_pic_index);
    3903     1526060 :                                 candidateArray[canTotalCnt].ref_frame_index_l0 = -1;
    3904     1526060 :                                 candidateArray[canTotalCnt].ref_frame_index_l1 = ref_pic_index;
    3905     1526060 :                                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    3906     1526060 :                                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    3907             : 
    3908     1526060 :                                 ChooseBestAv1MvPred(
    3909             :                                     context_ptr,
    3910     1526060 :                                     candidateArray[canTotalCnt].md_rate_estimation_ptr,
    3911             :                                     context_ptr->cu_ptr,
    3912     1526060 :                                     candidateArray[canTotalCnt].ref_frame_type,
    3913     1526060 :                                     candidateArray[canTotalCnt].is_compound,
    3914     1526060 :                                     candidateArray[canTotalCnt].pred_mode,
    3915     1526060 :                                     candidateArray[canTotalCnt].motion_vector_xl1,
    3916     1526060 :                                     candidateArray[canTotalCnt].motion_vector_yl1,
    3917             :                                     0, 0,
    3918     1526060 :                                     &candidateArray[canTotalCnt].drl_index,
    3919             :                                     bestPredmv);
    3920             : 
    3921     1526100 :                                 candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[0].as_mv.col;
    3922     1526100 :                                 candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[0].as_mv.row;
    3923             : #if OBMC_FLAG
    3924     1526100 :                                 if (inter_type == 0) {
    3925      961482 :                                     candidateArray[canTotalCnt].is_interintra_used = 0;
    3926      961482 :                                     candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    3927             :                                 }
    3928             :                                 else {
    3929      564613 :                                     if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    3930      564613 :                                         candidateArray[canTotalCnt].is_interintra_used = 0;
    3931      564613 :                                         candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    3932             : 
    3933      564613 :                                         obmc_motion_refinement(
    3934             :                                             picture_control_set_ptr,
    3935             :                                             context_ptr,
    3936      564613 :                                             &candidateArray[canTotalCnt],
    3937             :                                             REF_LIST_1);
    3938             :                                     }
    3939             :                                 }
    3940             : #endif
    3941     1526090 :                                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    3942     1526060 :                                 context_ptr->injected_mv_x_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_x;
    3943     1526060 :                                 context_ptr->injected_mv_y_l1_array[context_ptr->injected_mv_count_l1] = to_inject_mv_y;
    3944     1526060 :                                 context_ptr->injected_ref_type_l1_array[context_ptr->injected_mv_count_l1] = to_inject_ref_type;
    3945     1526060 :                                 ++context_ptr->injected_mv_count_l1;
    3946             : #if OBMC_FLAG
    3947             :                             }
    3948             : #endif
    3949             :                             }
    3950             :                         }
    3951             :                     }
    3952             :                 }
    3953             :             }
    3954             :             /**************
    3955             :                 NEW_NEWMV
    3956             :             ************* */
    3957      675302 :             if (allow_bipred) {
    3958             :                 uint8_t ref_pic_index_l0;
    3959             :                 uint8_t ref_pic_index_l1;
    3960             :                 {
    3961             :                     // Ref Picture Loop
    3962     2013900 :                     for (ref_pic_index_l0 = 0; ref_pic_index_l0 < 4; ++ref_pic_index_l0) {
    3963     8055370 :                         for (ref_pic_index_l1 = 0; ref_pic_index_l1 < 4; ++ref_pic_index_l1) {
    3964     6444260 :                             if (context_ptr->valid_refined_mv[REF_LIST_0][ref_pic_index_l0] && context_ptr->valid_refined_mv[REF_LIST_1][ref_pic_index_l1]) {
    3965     1727370 :                                 int16_t to_inject_mv_x_l0 = context_ptr->best_spatial_pred_mv[REF_LIST_0][ref_pic_index_l0][0];
    3966     1727370 :                                 int16_t to_inject_mv_y_l0 = context_ptr->best_spatial_pred_mv[REF_LIST_0][ref_pic_index_l0][1];
    3967     1727370 :                                 int16_t to_inject_mv_x_l1 = context_ptr->best_spatial_pred_mv[REF_LIST_1][ref_pic_index_l1][0];
    3968     1727370 :                                 int16_t to_inject_mv_y_l1 = context_ptr->best_spatial_pred_mv[REF_LIST_1][ref_pic_index_l1][1];
    3969             : 
    3970             :                                 MvReferenceFrame rf[2];
    3971     1727370 :                                 rf[0] = svt_get_ref_frame_type(REF_LIST_0, ref_pic_index_l0);
    3972     1727360 :                                 rf[1] = svt_get_ref_frame_type(REF_LIST_1, ref_pic_index_l1);
    3973     1727360 :                                 uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    3974     1727600 :                                 if (context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE) {
    3975             : 
    3976     1697060 :                                     context_ptr->variance_ready = 0;
    3977     7712620 :                                     for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    3978             :                                     {
    3979             :                                         // If two predictors are very similar, skip wedge compound mode search
    3980     6015790 :                                         if (context_ptr->variance_ready)
    3981           0 :                                             if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(NEW_NEWMV) && context_ptr->prediction_mse < 64))
    3982           0 :                                                 continue;
    3983             : 
    3984     6015790 :                                         candidateArray[canTotalCnt].type = INTER_MODE;
    3985     6015790 :                                         candidateArray[canTotalCnt].distortion_ready = 0;
    3986     6015790 :                                         candidateArray[canTotalCnt].use_intrabc = 0;
    3987     6015790 :                                         candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    3988     6015790 :                                         candidateArray[canTotalCnt].is_new_mv = 1;
    3989     6015790 :                                         candidateArray[canTotalCnt].is_zero_mv = 0;
    3990     6015790 :                                         candidateArray[canTotalCnt].drl_index = 0;
    3991             :                                         // Set the MV to ME result
    3992     6015790 :                                         candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    3993     6015790 :                                         candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    3994     6015790 :                                         candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    3995     6015790 :                                         candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    3996             :                                         // will be needed later by the rate estimation
    3997     6015790 :                                         candidateArray[canTotalCnt].ref_mv_index = 0;
    3998     6015790 :                                         candidateArray[canTotalCnt].pred_mv_weight = 0;
    3999     6015790 :                                         candidateArray[canTotalCnt].inter_mode = NEW_NEWMV;
    4000     6015790 :                                         candidateArray[canTotalCnt].pred_mode = NEW_NEWMV;
    4001     6015790 :                                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    4002     6015790 :                                         candidateArray[canTotalCnt].is_compound = 1;
    4003             : #if II_COMP_FLAG
    4004     6015790 :                                         candidateArray[canTotalCnt].is_interintra_used = 0;
    4005             : #endif
    4006     6015790 :                                         candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    4007             : 
    4008             :                                         MvReferenceFrame rf[2];
    4009     6015790 :                                         rf[0] = svt_get_ref_frame_type(REF_LIST_0, ref_pic_index_l0);
    4010     6015810 :                                         rf[1] = svt_get_ref_frame_type(REF_LIST_1, ref_pic_index_l1);
    4011     6015720 :                                         candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    4012     6015690 :                                         candidateArray[canTotalCnt].ref_frame_index_l0 = ref_pic_index_l0;
    4013     6015690 :                                         candidateArray[canTotalCnt].ref_frame_index_l1 = ref_pic_index_l1;
    4014             : 
    4015     6015690 :                                         candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    4016     6015690 :                                         candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    4017             : 
    4018     6015690 :                                         ChooseBestAv1MvPred(
    4019             :                                             context_ptr,
    4020     6015690 :                                             candidateArray[canTotalCnt].md_rate_estimation_ptr,
    4021             :                                             context_ptr->cu_ptr,
    4022     6015690 :                                             candidateArray[canTotalCnt].ref_frame_type,
    4023     6015690 :                                             candidateArray[canTotalCnt].is_compound,
    4024     6015690 :                                             candidateArray[canTotalCnt].pred_mode,
    4025     6015690 :                                             candidateArray[canTotalCnt].motion_vector_xl0,
    4026     6015690 :                                             candidateArray[canTotalCnt].motion_vector_yl0,
    4027     6015690 :                                             candidateArray[canTotalCnt].motion_vector_xl1,
    4028     6015690 :                                             candidateArray[canTotalCnt].motion_vector_yl1,
    4029     6015690 :                                             &candidateArray[canTotalCnt].drl_index,
    4030             :                                             bestPredmv);
    4031     6015470 :                                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_0] = bestPredmv[0].as_mv.col;
    4032     6015470 :                                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_0] = bestPredmv[0].as_mv.row;
    4033     6015470 :                                         candidateArray[canTotalCnt].motion_vector_pred_x[REF_LIST_1] = bestPredmv[1].as_mv.col;
    4034     6015470 :                                         candidateArray[canTotalCnt].motion_vector_pred_y[REF_LIST_1] = bestPredmv[1].as_mv.row;
    4035             : 
    4036             :                                         //MVP REFINE
    4037     6015470 :                                         determine_compound_mode(
    4038             :                                             picture_control_set_ptr,
    4039             :                                             context_ptr,
    4040     6015470 :                                             &candidateArray[canTotalCnt],
    4041             :                                             cur_type);
    4042     6015690 :                                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    4043     6015550 :                                         context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    4044     6015550 :                                         context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    4045     6015550 :                                         context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    4046     6015550 :                                         context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    4047             : 
    4048     6015550 :                                         context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    4049     6015550 :                                         ++context_ptr->injected_mv_count_bipred;
    4050             :                                     }
    4051             :                                 }
    4052             :                             }
    4053             :                         }
    4054             :                     }
    4055             :                 }
    4056             :             }
    4057             : 
    4058      675311 :             (*candidateTotalCnt) = canTotalCnt;
    4059      675311 :         }
    4060             : 
    4061      745068 : void  inject_inter_candidates(
    4062             :     PictureControlSet            *picture_control_set_ptr,
    4063             :     ModeDecisionContext          *context_ptr,
    4064             :     SsMeContext                  *ss_mecontext,
    4065             :     const SequenceControlSet     *sequence_control_set_ptr,
    4066             :     LargestCodingUnit            *sb_ptr,
    4067             :     uint32_t                       *candidateTotalCnt) {
    4068             : 
    4069             :     (void)sequence_control_set_ptr;
    4070             : 
    4071      745068 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    4072      745068 :     uint32_t                   canTotalCnt = *candidateTotalCnt;
    4073      745068 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    4074      745068 :     EbBool isCompoundEnabled = (frm_hdr->reference_mode == SINGLE_REFERENCE) ? 0 : 1;
    4075      745068 :     int inside_tile = 1;
    4076      745068 :     MacroBlockD  *xd = context_ptr->cu_ptr->av1xd;
    4077      745068 :     int umv0tile = (sequence_control_set_ptr->static_config.unrestricted_motion_vector == 0);
    4078      745068 :     MeLcuResults *me_results = picture_control_set_ptr->parent_pcs_ptr->me_results[context_ptr->me_sb_addr];
    4079             : 
    4080     1490140 :     EbBool use_close_loop_me = picture_control_set_ptr->parent_pcs_ptr->enable_in_loop_motion_estimation_flag &&
    4081      745068 :         ((context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) || (context_ptr->blk_geom->bwidth > 64 || context_ptr->blk_geom->bheight > 64)) ? EB_TRUE : EB_FALSE;
    4082             : 
    4083      745068 :     uint32_t close_loop_me_index = use_close_loop_me ? get_in_loop_me_info_index(MAX_SS_ME_PU_COUNT, sequence_control_set_ptr->seq_header.sb_size == BLOCK_128X128 ? 1 : 0, context_ptr->blk_geom) : 0;
    4084      745120 :     EbBool allow_bipred = (context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) ? EB_FALSE : EB_TRUE;
    4085      745120 :     uint8_t sq_index = LOG2F(context_ptr->blk_geom->sq_size) - 2;
    4086      745104 :     uint8_t inject_newmv_candidate = 1;
    4087             : 
    4088      745104 :     if (picture_control_set_ptr->parent_pcs_ptr->nsq_search_level >= NSQ_SEARCH_LEVEL1 &&
    4089      675249 :         picture_control_set_ptr->parent_pcs_ptr->nsq_search_level < NSQ_SEARCH_FULL) {
    4090      675253 :         if (context_ptr->md_local_cu_unit[context_ptr->blk_geom->sqi_mds].avail_blk_flag)
    4091     1193080 :             inject_newmv_candidate = context_ptr->blk_geom->shape == PART_N ? 1 :
    4092      596541 :                 context_ptr->parent_sq_has_coeff[sq_index] != 0 ? inject_newmv_candidate : 0;
    4093             :     }
    4094             : 
    4095             : #if FIX_COMPOUND
    4096      745104 :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    4097      745104 :     MD_COMP_TYPE compound_types_to_try = picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    4098             :     MD_COMP_TYPE cur_type; //GG
    4099     1367860 :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    4100      620031 :         (bsize >= BLOCK_8X8 && bsize <= BLOCK_32X32) ? compound_types_to_try :
    4101             :         (compound_types_to_try == MD_COMP_WEDGE) ? MD_COMP_DIFF0 :
    4102        2723 :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    4103             : #else
    4104             :     MD_COMP_TYPE cur_type; //GG
    4105             :     MD_COMP_TYPE tot_comp_types = picture_control_set_ptr->parent_pcs_ptr->compound_mode == 1 ? MD_COMP_AVG :
    4106             :         picture_control_set_ptr->parent_pcs_ptr->compound_types_to_try;
    4107             : #endif
    4108             : #if SPEED_OPT
    4109      745104 :     if (context_ptr->source_variance < context_ptr->inter_inter_wedge_variance_th)
    4110      189429 :         tot_comp_types = MIN(tot_comp_types, MD_COMP_DIFF0);
    4111             : #endif
    4112             : #if II_COMP_FLAG && !FIX_COMPOUND
    4113             :     BlockSize bsize = context_ptr->blk_geom->bsize;                       // bloc size
    4114             : #endif
    4115      745104 :     uint32_t mi_row = context_ptr->cu_origin_y >> MI_SIZE_LOG2;
    4116      745104 :     uint32_t mi_col = context_ptr->cu_origin_x >> MI_SIZE_LOG2;
    4117      745104 :     eb_av1_count_overlappable_neighbors(
    4118             :         picture_control_set_ptr,
    4119             :         context_ptr->cu_ptr,
    4120      745104 :         context_ptr->blk_geom->bsize,
    4121             :         mi_row,
    4122             :         mi_col);
    4123             : #if OBMC_FLAG
    4124      745145 :     uint8_t is_obmc_allowed = obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, context_ptr->blk_geom->bsize, LAST_FRAME, -1, NEWMV) == OBMC_CAUSAL;
    4125      745040 :     if (is_obmc_allowed)
    4126      399604 :         precompute_obmc_data(
    4127             :             picture_control_set_ptr,
    4128             :             context_ptr);
    4129             : #endif
    4130             :     /**************
    4131             :          MVP
    4132             :     ************* */
    4133             : 
    4134             :     uint32_t refIt;
    4135             :     //all of ref pairs: (1)single-ref List0  (2)single-ref List1  (3)compound Bi-Dir List0-List1  (4)compound Uni-Dir List0-List0  (5)compound Uni-Dir List1-List1
    4136     8587840 :     for (refIt = 0; refIt < picture_control_set_ptr->parent_pcs_ptr->tot_ref_frame_types; ++refIt) {
    4137     7842750 :         MvReferenceFrame ref_frame_pair = picture_control_set_ptr->parent_pcs_ptr->ref_frame_type_arr[refIt];
    4138     7842750 :         inject_mvp_candidates_II(
    4139             :             context_ptr,
    4140             :             picture_control_set_ptr,
    4141             :             context_ptr->cu_ptr,
    4142             :             ref_frame_pair,
    4143             :             &canTotalCnt);
    4144             :     }
    4145             : 
    4146             :     //----------------------
    4147             :     //    NEAREST_NEWMV, NEW_NEARESTMV, NEAR_NEWMV, NEW_NEARMV.
    4148             :     //----------------------
    4149      745092 :     if (context_ptr->new_nearest_near_comb_injection) {
    4150      465642 :         EbBool allow_compound = (frm_hdr->reference_mode == SINGLE_REFERENCE || context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) ? EB_FALSE : EB_TRUE;
    4151      465642 :         if (allow_compound) {
    4152             :             //all of ref pairs: (1)single-ref List0  (2)single-ref List1  (3)compound Bi-Dir List0-List1  (4)compound Uni-Dir List0-List0  (5)compound Uni-Dir List1-List1
    4153     2798200 :             for (refIt = 0; refIt < picture_control_set_ptr->parent_pcs_ptr->tot_ref_frame_types; ++refIt) {
    4154     2544340 :                 MvReferenceFrame ref_frame_pair = picture_control_set_ptr->parent_pcs_ptr->ref_frame_type_arr[refIt];
    4155     2544340 :                 inject_new_nearest_new_comb_candidates(
    4156             :                     sequence_control_set_ptr,
    4157             :                     context_ptr,
    4158             :                     picture_control_set_ptr,
    4159             :                     ref_frame_pair,
    4160             :                     &canTotalCnt);
    4161             :             }
    4162             :         }
    4163             :     }
    4164             : 
    4165      745064 :     if (inject_newmv_candidate) {
    4166             : 
    4167      222900 :         inject_new_candidates(
    4168             :             sequence_control_set_ptr,
    4169             :             context_ptr,
    4170             :             picture_control_set_ptr,
    4171             :             isCompoundEnabled,
    4172             :             allow_bipred,
    4173             :             context_ptr->me_sb_addr,
    4174             :             ss_mecontext,
    4175             :             use_close_loop_me,
    4176             :             close_loop_me_index,
    4177             :             context_ptr->me_block_offset,
    4178             :             &canTotalCnt);
    4179             : 
    4180      222909 :         if (context_ptr->nx4_4xn_parent_mv_injection) {
    4181             :             // If Nx4 or 4xN the inject the MV of the aprent block
    4182             : 
    4183             : 
    4184             :             // Derive whether if current block would need to have offsets made
    4185      153015 :             uint32_t bwidth_offset_to_8 = (context_ptr->blk_geom->bwidth == 4) << 2;
    4186      153015 :             uint32_t bheight_offset_to_8 = (context_ptr->blk_geom->bheight == 4) << 2;
    4187             : 
    4188             :             // if there is an offset needed to set either dimension to 8
    4189      153015 :             if (bwidth_offset_to_8 || bheight_offset_to_8) {
    4190             : 
    4191             :                 // Align parent block has dimensions inherited by current block, if current block has a dimension of 4
    4192             :                 // add 4 so the resulting block follows an 8x8 basis
    4193       54975 :                 uint32_t bwidth_to_search = context_ptr->blk_geom->bwidth + bwidth_offset_to_8;
    4194       54975 :                 uint32_t bheight_to_search = context_ptr->blk_geom->bheight + bheight_offset_to_8;
    4195             : 
    4196             :                 // Align parent block has origin inherited by current block
    4197       54975 :                 uint32_t x_to_search = context_ptr->blk_geom->origin_x - (context_ptr->geom_offset_x + ((context_ptr->blk_geom->origin_x & 0x7) ? 4 : 0));
    4198       54975 :                 uint32_t y_to_search = context_ptr->blk_geom->origin_y - (context_ptr->geom_offset_y + ((context_ptr->blk_geom->origin_y & 0x7) ? 4 : 0));
    4199             : 
    4200             :                 // Search the me_info_index of the parent block
    4201       54975 :                 uint32_t me_info_index = 0;
    4202     4046560 :                 for (uint32_t block_index = 0; block_index < picture_control_set_ptr->parent_pcs_ptr->max_number_of_pus_per_sb; block_index++) {
    4203             : 
    4204     4046560 :                     if (
    4205     4046560 :                         (bwidth_to_search == partition_width[block_index]) &&
    4206     1904720 :                         (bheight_to_search == partition_height[block_index]) &&
    4207     1245830 :                         (x_to_search == pu_search_index_map[block_index][0]) &&
    4208      201239 :                         (y_to_search == pu_search_index_map[block_index][1]))
    4209             :                     {
    4210       54974 :                         me_info_index = block_index;
    4211       54974 :                         break;
    4212             :                     }
    4213             :                 }
    4214             : 
    4215       54975 :                 inject_new_candidates(
    4216             :                     sequence_control_set_ptr,
    4217             :                     context_ptr,
    4218             :                     picture_control_set_ptr,
    4219             :                     isCompoundEnabled,
    4220             :                     allow_bipred,
    4221             :                     context_ptr->me_sb_addr,
    4222             :                     ss_mecontext,
    4223             :                     use_close_loop_me,
    4224             :                     close_loop_me_index,
    4225             :                     me_info_index,
    4226             :                     &canTotalCnt);
    4227             :             }
    4228             :         }
    4229             :     }
    4230             : 
    4231      745073 :     if (context_ptr->global_mv_injection) {
    4232             : #if GLOBAL_WARPED_MOTION
    4233     1350510 :         for (unsigned list_ref_index_l0 = 0; list_ref_index_l0 < 1; ++list_ref_index_l0)
    4234     1350510 :         for (unsigned list_ref_index_l1 = 0; list_ref_index_l1 < 1; ++list_ref_index_l1) {
    4235             : 
    4236             :             /**************
    4237             :              GLOBALMV
    4238             :             ************* */
    4239             : 
    4240      675255 :             uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, list_ref_index_l0);
    4241      675252 :             EbWarpedMotionParams *params_l0 = &picture_control_set_ptr->parent_pcs_ptr->global_motion[to_inject_ref_type];
    4242             : 
    4243      675252 :             IntMv mv_l0 = gm_get_motion_vector_enc(
    4244             :                 params_l0,
    4245      675252 :                 picture_control_set_ptr->parent_pcs_ptr->frm_hdr.allow_high_precision_mv,
    4246      675252 :                 context_ptr->blk_geom->bsize,
    4247             :                 mi_col, mi_row,
    4248             :                 0 /* force_integer_mv */);
    4249             : 
    4250      675262 :             int16_t to_inject_mv_x_l0 = mv_l0.as_mv.col;
    4251      675262 :             int16_t to_inject_mv_y_l0 = mv_l0.as_mv.row;
    4252             : 
    4253      675262 :             if(umv0tile)
    4254           0 :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l0,
    4255           0 :                                                       mi_col, mi_row, context_ptr->blk_geom->bsize);
    4256             : 
    4257      675262 :             if (inside_tile
    4258      675262 :                 && (((params_l0->wmtype > TRANSLATION
    4259      675261 :                       && context_ptr->blk_geom->bwidth >= 8
    4260      529485 :                       && context_ptr->blk_geom->bheight >= 8)
    4261      272475 :                      || params_l0->wmtype <= TRANSLATION))) {
    4262             : 
    4263             : #if II_COMP_FLAG      // GLOBALMV L0
    4264             :                  MvReferenceFrame rf[2];
    4265      402797 :                  rf[0] = to_inject_ref_type;
    4266      402797 :                  rf[1] = -1;
    4267             : 
    4268             :                 uint8_t inter_type;
    4269      402797 :                 uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, GLOBALMV, rf);
    4270      402787 :                 uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    4271             :                 //uint8_t is_obmc_allowed =  obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    4272             :                 //tot_inter_types = is_obmc_allowed ? tot_inter_types+1 : tot_inter_types;
    4273             : 
    4274     1397420 :                 for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    4275             :                 {
    4276             : #endif
    4277      994634 :                 candidateArray[canTotalCnt].type = INTER_MODE;
    4278             : 
    4279      994634 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    4280      994634 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    4281             : 
    4282      994634 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    4283      994634 :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    4284             : 
    4285      994634 :                 candidateArray[canTotalCnt].inter_mode = GLOBALMV;
    4286      994634 :                 candidateArray[canTotalCnt].pred_mode = GLOBALMV;
    4287      994634 :                 candidateArray[canTotalCnt].motion_mode = params_l0->wmtype > TRANSLATION ? WARPED_CAUSAL : SIMPLE_TRANSLATION;
    4288             : 
    4289      994634 :                 candidateArray[canTotalCnt].wm_params_l0 = *params_l0;
    4290             : 
    4291      994634 :                 candidateArray[canTotalCnt].is_compound = 0;
    4292      994634 :                 candidateArray[canTotalCnt].distortion_ready = 0;
    4293      994634 :                 candidateArray[canTotalCnt].use_intrabc = 0;
    4294      994634 :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    4295      994634 :                 candidateArray[canTotalCnt].prediction_direction[0] = UNI_PRED_LIST_0;
    4296      994634 :                 candidateArray[canTotalCnt].is_new_mv = 0;
    4297      994634 :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    4298      994634 :                 candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    4299      994634 :                 candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    4300      994634 :                 candidateArray[canTotalCnt].drl_index = 0;
    4301      994634 :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    4302      994634 :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    4303      994634 :                 candidateArray[canTotalCnt].ref_frame_type = av1_ref_frame_type(rf);
    4304      994629 :                 candidateArray[canTotalCnt].ref_frame_index_l0 = 0;
    4305      994629 :                 candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    4306      994629 :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    4307      994629 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    4308             : 
    4309             : #if II_COMP_FLAG
    4310      994629 :                 if (inter_type == 0) {
    4311      402785 :                     candidateArray[canTotalCnt].is_interintra_used = 0;
    4312             :                 }
    4313             :                 else {
    4314      591844 :                     if (is_ii_allowed) {
    4315      591862 :                         if (inter_type == 1) {
    4316      295933 :                             inter_intra_search(
    4317             :                                 picture_control_set_ptr,
    4318             :                                 context_ptr,
    4319      295933 :                                 &candidateArray[canTotalCnt]);
    4320      295936 :                             candidateArray[canTotalCnt].is_interintra_used = 1;
    4321      295936 :                             candidateArray[canTotalCnt].use_wedge_interintra = 1;
    4322      295936 :                             candidateArray[canTotalCnt].ii_wedge_sign = 0;
    4323             :                         }
    4324      295929 :                         else if (inter_type == 2) {
    4325      295936 :                             candidateArray[canTotalCnt].is_interintra_used = 1;
    4326      295936 :                             candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    4327      295936 :                             candidateArray[canTotalCnt].use_wedge_interintra = 0;
    4328             :                         }
    4329             :                     }
    4330             :                     //if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    4331             :                     //    candidateArray[canTotalCnt].is_interintra_used = 0;
    4332             :                     //    candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    4333             :                     //}
    4334             :                 }
    4335             : #endif
    4336             : 
    4337      994632 :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    4338             : 
    4339             : #if II_COMP_FLAG
    4340             :                 }
    4341             : #endif
    4342             : 
    4343      402784 :                 context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x_l0;
    4344      402784 :                 context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y_l0;
    4345      402784 :                 context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    4346      402784 :                 ++context_ptr->injected_mv_count_l0;
    4347             : 
    4348      805572 :                 EbWarpedMotionParams *params_l1 = &picture_control_set_ptr->parent_pcs_ptr
    4349      402784 :                         ->global_motion[svt_get_ref_frame_type(REF_LIST_1, list_ref_index_l1)];
    4350             : 
    4351      402788 :                 if (isCompoundEnabled && allow_bipred
    4352      382178 :                     && (params_l0->wmtype > TRANSLATION && params_l1->wmtype > TRANSLATION)) {
    4353             :                     /**************
    4354             :                     GLOBAL_GLOBALMV
    4355             :                     ************* */
    4356             : 
    4357      382178 :                     IntMv mv_l1 = gm_get_motion_vector_enc(
    4358             :                         params_l1,
    4359      382178 :                         picture_control_set_ptr->parent_pcs_ptr->frm_hdr.allow_high_precision_mv,
    4360      382178 :                         context_ptr->blk_geom->bsize,
    4361             :                         mi_col, mi_row,
    4362             :                         0 /* force_integer_mv */);
    4363             : 
    4364      382180 :                     int16_t to_inject_mv_x_l1 = mv_l1.as_mv.col;
    4365      382180 :                     int16_t to_inject_mv_y_l1 = mv_l1.as_mv.row;
    4366             : 
    4367      382180 :                     inside_tile = 1;
    4368      382180 :                     if (umv0tile) {
    4369           0 :                         inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l1,
    4370           0 :                                                               mi_col, mi_row, context_ptr->blk_geom->bsize)
    4371           0 :                                       && is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l1,
    4372           0 :                                                                  mi_col, mi_row, context_ptr->blk_geom->bsize);
    4373             :                     }
    4374             : 
    4375      382180 :                     if (inside_tile) {
    4376             :                         MvReferenceFrame rf[2];
    4377      382180 :                         rf[0] = svt_get_ref_frame_type(REF_LIST_0, list_ref_index_l0);
    4378      382180 :                         rf[1] = svt_get_ref_frame_type(REF_LIST_1, list_ref_index_l1);
    4379      382180 :                         uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    4380             : 
    4381             :                         // Warped prediction is only compatible with MD_COMP_AVG and MD_COMP_DIST.
    4382     1146520 :                         for (cur_type = MD_COMP_AVG; cur_type <= MIN(MD_COMP_DIST, tot_comp_types); cur_type++)
    4383             :                         {
    4384      764346 :                             candidateArray[canTotalCnt].type = INTER_MODE;
    4385      764346 :                             candidateArray[canTotalCnt].distortion_ready = 0;
    4386      764346 :                             candidateArray[canTotalCnt].use_intrabc = 0;
    4387             : 
    4388      764346 :                             candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    4389             : 
    4390      764346 :                             candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    4391             : 
    4392      764346 :                             candidateArray[canTotalCnt].inter_mode = GLOBAL_GLOBALMV;
    4393      764346 :                             candidateArray[canTotalCnt].pred_mode = GLOBAL_GLOBALMV;
    4394      764346 :                             candidateArray[canTotalCnt].motion_mode = params_l0->wmtype > TRANSLATION ? WARPED_CAUSAL : SIMPLE_TRANSLATION;
    4395      764346 :                             candidateArray[canTotalCnt].wm_params_l0 = *params_l0;
    4396      764346 :                             candidateArray[canTotalCnt].wm_params_l1 = *params_l1;
    4397      764346 :                             candidateArray[canTotalCnt].is_compound = 1;
    4398             : 
    4399             : #if II_COMP_FLAG
    4400      764346 :                             candidateArray[canTotalCnt].is_interintra_used = 0;
    4401             : #endif
    4402             : 
    4403      764346 :                             candidateArray[canTotalCnt].is_new_mv = 0;
    4404      764346 :                             candidateArray[canTotalCnt].is_zero_mv = 0;
    4405      764346 :                             candidateArray[canTotalCnt].drl_index = 0;
    4406             : 
    4407             :                             // will be needed later by the rate estimation
    4408      764346 :                             candidateArray[canTotalCnt].ref_mv_index = 0;
    4409      764346 :                             candidateArray[canTotalCnt].pred_mv_weight = 0;
    4410      764346 :                             candidateArray[canTotalCnt].ref_frame_type = to_inject_ref_type;
    4411      764346 :                             candidateArray[canTotalCnt].ref_frame_index_l0 = 0;
    4412      764346 :                             candidateArray[canTotalCnt].ref_frame_index_l1 = 0;
    4413      764346 :                             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    4414      764346 :                             candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    4415             :                             // Set the MV to frame MV
    4416             : 
    4417      764346 :                             candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    4418      764346 :                             candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    4419      764346 :                             candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    4420      764346 :                             candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    4421             :                             //GLOB-GLOB
    4422      764346 :                             determine_compound_mode(
    4423             :                                 picture_control_set_ptr,
    4424             :                                 context_ptr,
    4425      764346 :                                 &candidateArray[canTotalCnt],
    4426             :                                 cur_type);
    4427      764350 :                             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    4428             : 
    4429      764348 :                             context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    4430      764348 :                             context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    4431      764348 :                             context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    4432      764348 :                             context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    4433      764348 :                             context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    4434      764348 :                             ++context_ptr->injected_mv_count_bipred;
    4435             :                         }
    4436             :                     }
    4437             :                 }
    4438             :             }
    4439             :         }
    4440             : #else
    4441             :         /**************
    4442             :          GLOBALMV L0
    4443             :         ************* */
    4444             :         {
    4445             :             int16_t to_inject_mv_x = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[LAST_FRAME].wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF);
    4446             :             int16_t to_inject_mv_y = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[LAST_FRAME].wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF);
    4447             :             uint8_t to_inject_ref_type = svt_get_ref_frame_type(REF_LIST_0, 0/*list0_ref_index*/);
    4448             :             inj_mv = context_ptr->injected_mv_count_l0 == 0 || mrp_is_already_injected_mv_l0(context_ptr, to_inject_mv_x, to_inject_mv_y, to_inject_ref_type) == EB_FALSE;
    4449             :             if(umv0tile)
    4450             :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x, to_inject_mv_y, mi_col, mi_row, context_ptr->blk_geom->bsize);
    4451             :             inj_mv = inj_mv && inside_tile;
    4452             :             if (inj_mv) {
    4453             : 
    4454             : 
    4455             : #if II_COMP_FLAG      // GLOBALMV L0
    4456             :              MvReferenceFrame rf[2];
    4457             :              rf[0] = to_inject_ref_type;
    4458             :              rf[1] = -1;
    4459             : 
    4460             :             uint8_t inter_type;
    4461             :             uint8_t is_ii_allowed = svt_is_interintra_allowed(picture_control_set_ptr->parent_pcs_ptr->enable_inter_intra, bsize, GLOBALMV, rf);
    4462             :             uint8_t tot_inter_types = is_ii_allowed ? II_COUNT : 1;
    4463             :             //uint8_t is_obmc_allowed =  obmc_motion_mode_allowed(picture_control_set_ptr, context_ptr->cu_ptr, bsize, rf[0], rf[1], NEWMV) == OBMC_CAUSAL;
    4464             :             //tot_inter_types = is_obmc_allowed ? tot_inter_types+1 : tot_inter_types;
    4465             : 
    4466             :             for (inter_type = 0; inter_type < tot_inter_types; inter_type++)
    4467             :             {
    4468             : #endif
    4469             :                 candidateArray[canTotalCnt].type = INTER_MODE;
    4470             : 
    4471             :                 candidateArray[canTotalCnt].distortion_ready = 0;
    4472             :                 candidateArray[canTotalCnt].use_intrabc = 0;
    4473             : 
    4474             :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    4475             :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)0;
    4476             : 
    4477             :                 candidateArray[canTotalCnt].inter_mode = GLOBALMV;
    4478             :                 candidateArray[canTotalCnt].pred_mode = GLOBALMV;
    4479             :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    4480             :                 candidateArray[canTotalCnt].is_compound = 0;
    4481             :                 candidateArray[canTotalCnt].is_new_mv = 0;
    4482             :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    4483             :                 candidateArray[canTotalCnt].drl_index = 0;
    4484             : 
    4485             :                 // will be needed later by the rate estimation
    4486             :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    4487             :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    4488             :                 candidateArray[canTotalCnt].ref_frame_type = LAST_FRAME;
    4489             :                 candidateArray[canTotalCnt].ref_frame_index_l0 = 0;
    4490             :                 candidateArray[canTotalCnt].ref_frame_index_l1 = -1;
    4491             : 
    4492             :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    4493             :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    4494             :                 // Set the MV to frame MV
    4495             :                 candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x;
    4496             :                 candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y;
    4497             : 
    4498             : #if II_COMP_FLAG
    4499             :             if (inter_type == 0) {
    4500             :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    4501             :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    4502             :             }
    4503             :             else {
    4504             :                 if (is_ii_allowed) {
    4505             :                     if (inter_type == 1) {
    4506             :                         inter_intra_search(
    4507             :                             picture_control_set_ptr,
    4508             :                             context_ptr,
    4509             :                             &candidateArray[canTotalCnt]);
    4510             :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    4511             :                         candidateArray[canTotalCnt].use_wedge_interintra = 1;
    4512             :                         candidateArray[canTotalCnt].ii_wedge_sign = 0;
    4513             :                     }
    4514             :                     else if (inter_type == 2) {
    4515             :                         candidateArray[canTotalCnt].is_interintra_used = 1;
    4516             :                         candidateArray[canTotalCnt].interintra_mode = candidateArray[canTotalCnt - 1].interintra_mode;
    4517             :                         candidateArray[canTotalCnt].use_wedge_interintra = 0;
    4518             :                     }
    4519             :                 }
    4520             :                 //if (is_obmc_allowed && inter_type == tot_inter_types - 1) {
    4521             :                 //    candidateArray[canTotalCnt].is_interintra_used = 0;
    4522             :                 //    candidateArray[canTotalCnt].motion_mode = OBMC_CAUSAL;
    4523             :                 //}
    4524             :             }
    4525             : #endif
    4526             :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    4527             : 
    4528             : #if II_COMP_FLAG
    4529             :             }
    4530             : #endif
    4531             :                 context_ptr->injected_mv_x_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_x;
    4532             :                 context_ptr->injected_mv_y_l0_array[context_ptr->injected_mv_count_l0] = to_inject_mv_y;
    4533             :                 context_ptr->injected_ref_type_l0_array[context_ptr->injected_mv_count_l0] = to_inject_ref_type;
    4534             :                 ++context_ptr->injected_mv_count_l0;
    4535             :             }
    4536             :         }
    4537             : 
    4538             :         if (isCompoundEnabled && allow_bipred) {
    4539             :             /**************
    4540             :             GLOBAL_GLOBALMV
    4541             :             ************* */
    4542             : 
    4543             :             int16_t to_inject_mv_x_l0 = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[LAST_FRAME].wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF);
    4544             :             int16_t to_inject_mv_y_l0 = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[LAST_FRAME].wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF);
    4545             :             int16_t to_inject_mv_x_l1 = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[BWDREF_FRAME].wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF);
    4546             :             int16_t to_inject_mv_y_l1 = (int16_t)(picture_control_set_ptr->parent_pcs_ptr->global_motion[BWDREF_FRAME].wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF);
    4547             :             MvReferenceFrame rf[2];
    4548             :             rf[0] = svt_get_ref_frame_type(REF_LIST_0, 0/*list0_ref_index*/);
    4549             :             rf[1] = svt_get_ref_frame_type(REF_LIST_1, 0/*list1_ref_index*/);
    4550             :             uint8_t to_inject_ref_type = av1_ref_frame_type(rf);
    4551             :             inside_tile = 1;
    4552             :             if(umv0tile) {
    4553             :                 inside_tile = is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize) &&
    4554             :                               is_inside_tile_boundary(&(xd->tile), to_inject_mv_x_l0, to_inject_mv_y_l1, mi_col, mi_row, context_ptr->blk_geom->bsize);
    4555             :             }
    4556             :             if (inside_tile && (context_ptr->injected_mv_count_bipred == 0 || mrp_is_already_injected_mv_bipred(context_ptr, to_inject_mv_x_l0, to_inject_mv_y_l0, to_inject_mv_x_l1, to_inject_mv_y_l1, to_inject_ref_type) == EB_FALSE)) {
    4557             :                 context_ptr->variance_ready = 0;
    4558             :                 for (cur_type = MD_COMP_AVG; cur_type <= tot_comp_types; cur_type++)
    4559             :                 {
    4560             :                     if (cur_type == MD_COMP_WEDGE && wedge_params_lookup[context_ptr->blk_geom->bsize].bits == 0) continue;
    4561             :                     // If two predictors are very similar, skip wedge compound mode search
    4562             :                     if (context_ptr->variance_ready)
    4563             :                         if (context_ptr->prediction_mse < 8 || (!have_newmv_in_inter_mode(GLOBAL_GLOBALMV) && context_ptr->prediction_mse < 64))
    4564             :                             continue;
    4565             :                 candidateArray[canTotalCnt].type = INTER_MODE;
    4566             :                 candidateArray[canTotalCnt].distortion_ready = 0;
    4567             :                 candidateArray[canTotalCnt].use_intrabc = 0;
    4568             : 
    4569             :                 candidateArray[canTotalCnt].merge_flag = EB_FALSE;
    4570             : 
    4571             :                 candidateArray[canTotalCnt].prediction_direction[0] = (EbPredDirection)2;
    4572             : 
    4573             :                 candidateArray[canTotalCnt].inter_mode = GLOBAL_GLOBALMV;
    4574             :                 candidateArray[canTotalCnt].pred_mode = GLOBAL_GLOBALMV;
    4575             :                 candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    4576             :                 candidateArray[canTotalCnt].is_compound = 1;
    4577             : #if II_COMP_FLAG
    4578             :                 candidateArray[canTotalCnt].is_interintra_used = 0;
    4579             : #endif
    4580             :                 candidateArray[canTotalCnt].is_new_mv = 0;
    4581             :                 candidateArray[canTotalCnt].is_zero_mv = 0;
    4582             :                 candidateArray[canTotalCnt].drl_index = 0;
    4583             : 
    4584             :                 // will be needed later by the rate estimation
    4585             :                 candidateArray[canTotalCnt].ref_mv_index = 0;
    4586             :                 candidateArray[canTotalCnt].pred_mv_weight = 0;
    4587             :                 candidateArray[canTotalCnt].ref_frame_type = LAST_BWD_FRAME;
    4588             :                 candidateArray[canTotalCnt].ref_frame_index_l0 = 0;
    4589             :                 candidateArray[canTotalCnt].ref_frame_index_l1 = 0;
    4590             :                 candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    4591             :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    4592             :                 // Set the MV to frame MV
    4593             : 
    4594             :                 candidateArray[canTotalCnt].motion_vector_xl0 = to_inject_mv_x_l0;
    4595             :                 candidateArray[canTotalCnt].motion_vector_yl0 = to_inject_mv_y_l0;
    4596             :                 candidateArray[canTotalCnt].motion_vector_xl1 = to_inject_mv_x_l1;
    4597             :                 candidateArray[canTotalCnt].motion_vector_yl1 = to_inject_mv_y_l1;
    4598             :                 //GLOB-GLOB
    4599             :                 determine_compound_mode(
    4600             :                     picture_control_set_ptr,
    4601             :                     context_ptr,
    4602             :                     &candidateArray[canTotalCnt],
    4603             :                     cur_type);
    4604             :                 INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    4605             : 
    4606             :                 context_ptr->injected_mv_x_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l0;
    4607             :                 context_ptr->injected_mv_y_bipred_l0_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l0;
    4608             :                 context_ptr->injected_mv_x_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_x_l1;
    4609             :                 context_ptr->injected_mv_y_bipred_l1_array[context_ptr->injected_mv_count_bipred] = to_inject_mv_y_l1;
    4610             :                 context_ptr->injected_ref_type_bipred_array[context_ptr->injected_mv_count_bipred] = to_inject_ref_type;
    4611             :                 ++context_ptr->injected_mv_count_bipred;
    4612             :                 }
    4613             :             }
    4614             :         }
    4615             : #endif
    4616             :     }
    4617             : 
    4618             :     // Warped Motion
    4619     1210720 :     if (frm_hdr->allow_warped_motion &&
    4620      465646 :         has_overlappable_candidates(context_ptr->cu_ptr) &&
    4621      264852 :         context_ptr->blk_geom->bwidth >= 8 &&
    4622      264852 :         context_ptr->blk_geom->bheight >= 8 &&
    4623      264852 :         context_ptr->warped_motion_injection) {
    4624      264852 :         inject_warped_motion_candidates(
    4625             :             picture_control_set_ptr,
    4626             :             context_ptr,
    4627             :             context_ptr->cu_ptr,
    4628             :             &canTotalCnt,
    4629             :             ss_mecontext,
    4630             :             me_results,
    4631             :             use_close_loop_me,
    4632             :             close_loop_me_index);
    4633             :     }
    4634             : 
    4635      745070 :     if (inject_newmv_candidate) {
    4636      222876 :         if (isCompoundEnabled) {
    4637      211885 :             if (allow_bipred) {
    4638             : 
    4639             :             //----------------------
    4640             :             // Bipred2Nx2N
    4641             :             //----------------------
    4642      158985 :             if (context_ptr->bipred3x3_injection > 0)
    4643       92804 :                 if (picture_control_set_ptr->slice_type == B_SLICE)
    4644       92804 :                     Bipred3x3CandidatesInjection(
    4645             :                         sequence_control_set_ptr,
    4646             :                         picture_control_set_ptr,
    4647             :                         context_ptr,
    4648             :                         sb_ptr,
    4649             :                         context_ptr->me_sb_addr,
    4650             :                         ss_mecontext,
    4651             :                         use_close_loop_me,
    4652             :                         close_loop_me_index,
    4653             :                         &canTotalCnt);
    4654             : 
    4655             :         }
    4656             : 
    4657             :         //----------------------
    4658             :         // Unipred2Nx2N
    4659             :         //----------------------
    4660      211885 :         if (context_ptr->unipred3x3_injection > 0)
    4661      145717 :             if (picture_control_set_ptr->slice_type != I_SLICE)
    4662      145717 :                 Unipred3x3CandidatesInjection(
    4663             :                     sequence_control_set_ptr,
    4664             :                     picture_control_set_ptr,
    4665             :                     context_ptr,
    4666             :                     sb_ptr,
    4667             :                     context_ptr->me_sb_addr,
    4668             :                     ss_mecontext,
    4669             :                     use_close_loop_me,
    4670             :                     close_loop_me_index,
    4671             :                     &canTotalCnt);
    4672             :             }
    4673             :         }
    4674             : 
    4675             : #if EIGTH_PEL_MV && !EIGHT_PEL_PREDICTIVE_ME
    4676             :         //----------------------
    4677             :         // Eighth-pel refinement
    4678             :         //----------------------
    4679             :         if (inject_newmv_candidate && picture_control_set_ptr->parent_pcs_ptr->allow_high_precision_mv) {
    4680             :             if (allow_bipred) {
    4681             :                 //----------------------
    4682             :                 // Inject eight-pel bi-pred
    4683             :                 //----------------------
    4684             :                 if (context_ptr->bipred3x3_injection > 0)
    4685             :                     if (picture_control_set_ptr->slice_type == B_SLICE)
    4686             :                         eighth_pel_bipred_refinement(
    4687             :                             sequence_control_set_ptr,
    4688             :                             picture_control_set_ptr,
    4689             :                             context_ptr,
    4690             :                             me_sb_addr,
    4691             :                             ss_mecontext,
    4692             :                             use_close_loop_me,
    4693             :                             close_loop_me_index,
    4694             :                             &canTotalCnt);
    4695             : 
    4696             :             //----------------------
    4697             :             // Inject eight-pel uni-pred
    4698             :             //----------------------
    4699             :             if (context_ptr->unipred3x3_injection > 0)
    4700             :                 if (picture_control_set_ptr->slice_type != I_SLICE)
    4701             :                     eighth_pel_unipred_refinement(
    4702             :                         sequence_control_set_ptr,
    4703             :                         picture_control_set_ptr,
    4704             :                         context_ptr,
    4705             :                         me_sb_addr,
    4706             :                         ss_mecontext,
    4707             :                         use_close_loop_me,
    4708             :                         close_loop_me_index,
    4709             :                         &canTotalCnt);
    4710             :                 }
    4711             :             }
    4712             : #endif
    4713             : 
    4714      745071 :         if (context_ptr->predictive_me_level)
    4715      675254 :             inject_predictive_me_candidates(
    4716             :                 context_ptr,
    4717             :                 picture_control_set_ptr,
    4718             :                 isCompoundEnabled,
    4719             :                 allow_bipred,
    4720             :                 &canTotalCnt);
    4721             : // update the total number of candidates injected
    4722      745131 : (*candidateTotalCnt) = canTotalCnt;
    4723             : 
    4724      745131 : return;
    4725             :     }
    4726             : 
    4727      762457 : static INLINE TxType av1_get_tx_type(
    4728             :     BlockSize  sb_type,
    4729             :     int32_t   is_inter,
    4730             :     PredictionMode pred_mode,
    4731             :     UvPredictionMode pred_mode_uv,
    4732             :     PlaneType plane_type,
    4733             :     const MacroBlockD *xd, int32_t blk_row,
    4734             :     int32_t blk_col, TxSize tx_size,
    4735             :     int32_t reduced_tx_set)
    4736             : {
    4737             :     UNUSED(sb_type);
    4738             :     UNUSED(*xd);
    4739             :     UNUSED(blk_row);
    4740             :     UNUSED(blk_col);
    4741             : 
    4742             :     // BlockSize  sb_type = BLOCK_8X8;
    4743             : 
    4744             :     MbModeInfo  mbmi;
    4745      762457 :     mbmi.block_mi.mode = pred_mode;
    4746      762457 :     mbmi.block_mi.uv_mode = pred_mode_uv;
    4747             : 
    4748             :     // const MbModeInfo *const mbmi = xd->mi[0];
    4749             :     // const struct MacroblockdPlane *const pd = &xd->plane[plane_type];
    4750             :     const TxSetType tx_set_type =
    4751      762457 :         /*av1_*/get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set);
    4752             : 
    4753      762417 :     TxType tx_type = DCT_DCT;
    4754      762417 :     if ( /*xd->lossless[mbmi->segment_id] ||*/ txsize_sqr_up_map[tx_size] > TX_32X32)
    4755           0 :         tx_type = DCT_DCT;
    4756             :     else {
    4757      762417 :         if (plane_type == PLANE_TYPE_Y) {
    4758             :             //const int32_t txk_type_idx =
    4759             :             //    av1_get_txk_type_index(/*mbmi->*/sb_type, blk_row, blk_col);
    4760             :             //tx_type = mbmi->txk_type[txk_type_idx];
    4761             :         }
    4762      762429 :         else if (is_inter /*is_inter_block(mbmi)*/) {
    4763             :             // scale back to y plane's coordinate
    4764             :             //blk_row <<= pd->subsampling_y;
    4765             :             //blk_col <<= pd->subsampling_x;
    4766             :             //const int32_t txk_type_idx =
    4767             :             //    av1_get_txk_type_index(mbmi->sb_type, blk_row, blk_col);
    4768             :             //tx_type = mbmi->txk_type[txk_type_idx];
    4769             :         }
    4770             :         else {
    4771             :             // In intra mode, uv planes don't share the same prediction mode as y
    4772             :             // plane, so the tx_type should not be shared
    4773      762429 :             tx_type = intra_mode_to_tx_type(&mbmi.block_mi, PLANE_TYPE_UV);
    4774             :         }
    4775             :     }
    4776             :     ASSERT(tx_type < TX_TYPES);
    4777      762387 :     if (!av1_ext_tx_used[tx_set_type][tx_type]) return DCT_DCT;
    4778      314851 :     return tx_type;
    4779             : }
    4780             : 
    4781           0 : void  inject_intra_candidates_ois(
    4782             :     PictureControlSet            *picture_control_set_ptr,
    4783             :     ModeDecisionContext          *context_ptr,
    4784             :     LargestCodingUnit            *sb_ptr,
    4785             :     uint32_t                       *candidate_total_cnt){
    4786             :     uint8_t                     intra_candidate_counter;
    4787             :     uint8_t                     intra_mode;
    4788           0 :     uint32_t                    can_total_cnt = 0;
    4789           0 :     ModeDecisionCandidate    *candidate_array = context_ptr->fast_candidate_array;
    4790           0 :     EbBool                      disable_cfl_flag = (MAX(context_ptr->blk_geom->bheight, context_ptr->blk_geom->bwidth) > 32) ? EB_TRUE : EB_FALSE;
    4791             : 
    4792           0 :     OisSbResults    *ois_sb_results_ptr = picture_control_set_ptr->parent_pcs_ptr->ois_sb_results[sb_ptr->index];
    4793           0 :     OisCandidate     *ois_blk_ptr = ois_sb_results_ptr->ois_candidate_array[ep_to_pa_block_index[context_ptr->blk_geom->blkidx_mds]];
    4794           0 :     uint8_t              total_intra_luma_mode = ois_sb_results_ptr-> total_ois_intra_candidate[ep_to_pa_block_index[context_ptr->blk_geom->blkidx_mds]];
    4795           0 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    4796             : 
    4797           0 :     for (intra_candidate_counter = 0; intra_candidate_counter < total_intra_luma_mode; ++intra_candidate_counter) {
    4798           0 :         intra_mode = ois_blk_ptr[can_total_cnt].intra_mode;
    4799           0 :         assert(intra_mode < INTRA_MODES);
    4800           0 :         if (av1_is_directional_mode((PredictionMode)intra_mode)) {
    4801           0 :             int32_t angle_delta = ois_blk_ptr[can_total_cnt].angle_delta ;
    4802           0 :             candidate_array[can_total_cnt].type = INTRA_MODE;
    4803             : #if PAL_SUP
    4804           0 :             candidate_array[can_total_cnt].palette_info.pmi.palette_size[0] = 0;
    4805           0 :             candidate_array[can_total_cnt].palette_info.pmi.palette_size[1] = 0;
    4806             : #endif
    4807           0 :             candidate_array[can_total_cnt].intra_luma_mode = intra_mode;
    4808           0 :             candidate_array[can_total_cnt].distortion_ready =  1;
    4809           0 :             candidate_array[can_total_cnt].me_distortion = ois_blk_ptr[can_total_cnt].distortion;
    4810           0 :             candidate_array[can_total_cnt].use_intrabc = 0;
    4811           0 :             candidate_array[can_total_cnt].is_directional_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)intra_mode);
    4812           0 :             candidate_array[can_total_cnt].angle_delta[PLANE_TYPE_Y] = angle_delta;
    4813           0 :             candidate_array[can_total_cnt].intra_chroma_mode = disable_cfl_flag ? intra_luma_to_chroma[intra_mode] :
    4814           0 :                                                                context_ptr->chroma_level <= CHROMA_MODE_1 ? UV_CFL_PRED : UV_DC_PRED;
    4815             : 
    4816           0 :             candidate_array[can_total_cnt].cfl_alpha_signs = 0;
    4817           0 :             candidate_array[can_total_cnt].cfl_alpha_idx = 0;
    4818           0 :             candidate_array[can_total_cnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidate_array[can_total_cnt].intra_chroma_mode);
    4819           0 :             candidate_array[can_total_cnt].angle_delta[PLANE_TYPE_UV] = 0;
    4820             : 
    4821           0 :             candidate_array[can_total_cnt].transform_type[0] = DCT_DCT;
    4822             : 
    4823           0 :             if (candidate_array[can_total_cnt].intra_chroma_mode == UV_CFL_PRED)
    4824           0 :                 candidate_array[can_total_cnt].transform_type_uv = DCT_DCT;
    4825             :             else
    4826           0 :                 candidate_array[can_total_cnt].transform_type_uv =
    4827           0 :                 av1_get_tx_type(
    4828           0 :                     context_ptr->blk_geom->bsize,
    4829             :                     0,
    4830           0 :                     (PredictionMode)candidate_array[can_total_cnt].intra_luma_mode,
    4831           0 :                     (UvPredictionMode)candidate_array[can_total_cnt].intra_chroma_mode,
    4832             :                     PLANE_TYPE_UV,
    4833             :                     0,
    4834             :                     0,
    4835             :                     0,
    4836           0 :                     context_ptr->blk_geom->txsize_uv[0][0],
    4837           0 :                     frm_hdr->reduced_tx_set);
    4838           0 :             candidate_array[can_total_cnt].ref_frame_type = INTRA_FRAME;
    4839           0 :             candidate_array[can_total_cnt].pred_mode = (PredictionMode)intra_mode;
    4840           0 :             candidate_array[can_total_cnt].motion_mode = SIMPLE_TRANSLATION;
    4841           0 :             INCRMENT_CAND_TOTAL_COUNT(can_total_cnt);
    4842             :         }
    4843             :         else {
    4844           0 :             candidate_array[can_total_cnt].type = INTRA_MODE;
    4845             : #if PAL_SUP
    4846           0 :             candidate_array[can_total_cnt].palette_info.pmi.palette_size[0] = 0;
    4847           0 :             candidate_array[can_total_cnt].palette_info.pmi.palette_size[1] = 0;
    4848             : #endif
    4849           0 :             candidate_array[can_total_cnt].intra_luma_mode = intra_mode;
    4850           0 :             candidate_array[can_total_cnt].distortion_ready =  1;
    4851           0 :             candidate_array[can_total_cnt].me_distortion = ois_blk_ptr[can_total_cnt].distortion;
    4852           0 :             candidate_array[can_total_cnt].use_intrabc = 0;
    4853           0 :             candidate_array[can_total_cnt].is_directional_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)intra_mode);
    4854           0 :             candidate_array[can_total_cnt].angle_delta[PLANE_TYPE_Y] = 0;
    4855           0 :             candidate_array[can_total_cnt].intra_chroma_mode =  disable_cfl_flag ? intra_luma_to_chroma[intra_mode] :
    4856           0 :                                                                 context_ptr->chroma_level <= CHROMA_MODE_1 ? UV_CFL_PRED : UV_DC_PRED;
    4857             : 
    4858           0 :             candidate_array[can_total_cnt].cfl_alpha_signs = 0;
    4859           0 :             candidate_array[can_total_cnt].cfl_alpha_idx = 0;
    4860           0 :             candidate_array[can_total_cnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidate_array[can_total_cnt].intra_chroma_mode);
    4861           0 :             candidate_array[can_total_cnt].angle_delta[PLANE_TYPE_UV] = 0;
    4862           0 :             candidate_array[can_total_cnt].transform_type[0] = DCT_DCT;
    4863             : 
    4864           0 :             if (candidate_array[can_total_cnt].intra_chroma_mode == UV_CFL_PRED)
    4865           0 :                 candidate_array[can_total_cnt].transform_type_uv = DCT_DCT;
    4866             :             else
    4867           0 :                 candidate_array[can_total_cnt].transform_type_uv =
    4868           0 :                 av1_get_tx_type(
    4869           0 :                     context_ptr->blk_geom->bsize,
    4870             :                     0,
    4871           0 :                     (PredictionMode)candidate_array[can_total_cnt].intra_luma_mode,
    4872           0 :                     (UvPredictionMode)candidate_array[can_total_cnt].intra_chroma_mode,
    4873             :                     PLANE_TYPE_UV,
    4874             :                     0,
    4875             :                     0,
    4876             :                     0,
    4877           0 :                     context_ptr->blk_geom->txsize_uv[0][0],
    4878           0 :                     frm_hdr->reduced_tx_set);
    4879           0 :             candidate_array[can_total_cnt].ref_frame_type = INTRA_FRAME;
    4880           0 :             candidate_array[can_total_cnt].pred_mode = (PredictionMode)intra_mode;
    4881           0 :             candidate_array[can_total_cnt].motion_mode = SIMPLE_TRANSLATION;
    4882           0 :             INCRMENT_CAND_TOTAL_COUNT(can_total_cnt);
    4883             :         }
    4884             :     }
    4885             : 
    4886             :     // update the total number of candidates injected
    4887           0 :     (*candidate_total_cnt) = can_total_cnt;
    4888             : 
    4889           0 :     return;
    4890             : }
    4891             : 
    4892             : double eb_av1_convert_qindex_to_q(int32_t qindex, AomBitDepth bit_depth);
    4893             : 
    4894             : #if !OBMC_FLAG
    4895             : static INLINE void setup_pred_plane(struct Buf2D *dst, BlockSize bsize,
    4896             :     uint8_t *src, int width, int height,
    4897             :     int stride, int mi_row, int mi_col,
    4898             :     int subsampling_x, int subsampling_y) {
    4899             :     // Offset the buffer pointer
    4900             :     if (subsampling_y && (mi_row & 0x01) && (mi_size_high[bsize] == 1))
    4901             :         mi_row -= 1;
    4902             :     if (subsampling_x && (mi_col & 0x01) && (mi_size_wide[bsize] == 1))
    4903             :         mi_col -= 1;
    4904             : 
    4905             :     const int x = (MI_SIZE * mi_col) >> subsampling_x;
    4906             :     const int y = (MI_SIZE * mi_row) >> subsampling_y;
    4907             :     dst->buf = src + (y * stride + x);// scaled_buffer_offset(x, y, stride, scale);
    4908             :     dst->buf0 = src;
    4909             :     dst->width = width;
    4910             :     dst->height = height;
    4911             :     dst->stride = stride;
    4912             : }
    4913             : void eb_av1_setup_pred_block(BlockSize sb_type,
    4914             :     struct Buf2D dst[MAX_MB_PLANE],
    4915             :     const Yv12BufferConfig *src, int mi_row, int mi_col) {
    4916             :     int i;
    4917             : 
    4918             :     dst[0].buf = src->y_buffer;
    4919             :     dst[0].stride = src->y_stride;
    4920             :     dst[1].buf = src->u_buffer;
    4921             :     dst[2].buf = src->v_buffer;
    4922             :     dst[1].stride = dst[2].stride = src->uv_stride;
    4923             : 
    4924             :     i = 0;
    4925             :     setup_pred_plane(dst + i, sb_type, dst[i].buf,
    4926             :         i ? src->uv_crop_width : src->y_crop_width,
    4927             :         i ? src->uv_crop_height : src->y_crop_height,
    4928             :         dst[i].stride, mi_row, mi_col,
    4929             :         0, 0);
    4930             : }
    4931             : #endif
    4932             : // Values are now correlated to quantizer.
    4933             : static int sad_per_bit16lut_8[QINDEX_RANGE];
    4934             : static int sad_per_bit4lut_8[QINDEX_RANGE];
    4935             : 
    4936           2 : static void init_me_luts_bd(int *bit16lut, int *bit4lut, int range,
    4937             :     AomBitDepth bit_depth) {
    4938             :     int i;
    4939             :     // Initialize the sad lut tables using a formulaic calculation for now.
    4940             :     // This is to make it easier to resolve the impact of experimental changes
    4941             :     // to the quantizer tables.
    4942         514 :     for (i = 0; i < range; i++) {
    4943         512 :         const double q = eb_av1_convert_qindex_to_q(i, bit_depth);
    4944         512 :         bit16lut[i] = (int)(0.0418 * q + 2.4107);
    4945         512 :         bit4lut[i] = (int)(0.063 * q + 2.742);
    4946             :     }
    4947           2 : }
    4948             : 
    4949           2 : void eb_av1_init_me_luts(void) {
    4950           2 :     init_me_luts_bd(sad_per_bit16lut_8, sad_per_bit4lut_8, QINDEX_RANGE,
    4951             :         AOM_BITS_8);
    4952           2 : }
    4953             : 
    4954           0 : static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) {
    4955           0 :     return (mv->row >> 3) < mv_limits->row_min ||
    4956           0 :         (mv->row >> 3) > mv_limits->row_max ||
    4957           0 :         (mv->col >> 3) < mv_limits->col_min ||
    4958           0 :         (mv->col >> 3) > mv_limits->col_max;
    4959             : }
    4960           0 : void assert_release(int statement)
    4961             : {
    4962           0 :     if (statement == 0)
    4963           0 :         printf("ASSERT_ERRRR\n");
    4964           0 : }
    4965             : 
    4966           0 : void  intra_bc_search(
    4967             :     PictureControlSet            *pcs,
    4968             :     ModeDecisionContext          *context_ptr,
    4969             :     const SequenceControlSet     *scs,
    4970             :     CodingUnit                   *cu_ptr,
    4971             :     MV                             *dv_cand,
    4972             :     uint8_t                        *num_dv_cand)
    4973             : {
    4974             :     IntraBcContext  x_st;
    4975           0 :     IntraBcContext  *x = &x_st;
    4976             :     //fill x with what needed.
    4977           0 :     x->is_exhaustive_allowed =  context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4 ? 1 : 0;
    4978             :     //CHKN crc calculator could be moved to mdContext and these init at init time.
    4979           0 :     av1_crc_calculator_init(&x->crc_calculator1, 24, 0x5D6DCB);
    4980           0 :     av1_crc_calculator_init(&x->crc_calculator2, 24, 0x864CFB);
    4981             : 
    4982           0 :     x->xd = cu_ptr->av1xd;
    4983           0 :     x->nmv_vec_cost = context_ptr->md_rate_estimation_ptr->nmv_vec_cost;
    4984           0 :     x->mv_cost_stack = context_ptr->md_rate_estimation_ptr->nmvcoststack;
    4985           0 :     BlockSize bsize = context_ptr->blk_geom->bsize;
    4986           0 :     assert(bsize < BlockSizeS_ALL);
    4987           0 :     FrameHeader *frm_hdr = &pcs->parent_pcs_ptr->frm_hdr;
    4988           0 :     const Av1Common *const cm = pcs->parent_pcs_ptr->av1_cm;
    4989           0 :     MvReferenceFrame ref_frame = INTRA_FRAME;
    4990           0 :     const int num_planes = 3;
    4991           0 :     MacroBlockD * xd = cu_ptr->av1xd;
    4992           0 :     const TileInfo *tile = &xd->tile;
    4993           0 :     const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE);
    4994           0 :     const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE);
    4995           0 :     const int w = block_size_wide[bsize];
    4996           0 :     const int h = block_size_high[bsize];
    4997           0 :     const int sb_row = mi_row >> scs->seq_header.sb_size_log2;
    4998           0 :     const int sb_col = mi_col >> scs->seq_header.sb_size_log2;
    4999             : 
    5000             :     // Set up limit values for MV components.
    5001             :     // Mv beyond the range do not produce new/different prediction block.
    5002           0 :     const int mi_width = mi_size_wide[bsize];
    5003           0 :     const int mi_height = mi_size_high[bsize];
    5004           0 :     x->mv_limits.row_min =
    5005           0 :         -(((mi_row + mi_height) * MI_SIZE) + AOM_INTERP_EXTEND);
    5006           0 :     x->mv_limits.col_min = -(((mi_col + mi_width) * MI_SIZE) + AOM_INTERP_EXTEND);
    5007           0 :     x->mv_limits.row_max = (cm->mi_rows - mi_row) * MI_SIZE + AOM_INTERP_EXTEND;
    5008           0 :     x->mv_limits.col_max = (cm->mi_cols - mi_col) * MI_SIZE + AOM_INTERP_EXTEND;
    5009             :     //set search paramters
    5010           0 :     x->sadperbit16 = sad_per_bit16lut_8[frm_hdr->quantization_params.base_q_idx];
    5011           0 :     x->errorperbit = context_ptr->full_lambda >> RD_EPB_SHIFT;
    5012           0 :     x->errorperbit += (x->errorperbit == 0);
    5013             :     //temp buffer for hash me
    5014           0 :     for (int xi = 0; xi < 2; xi++)
    5015           0 :         for (int yj = 0; yj < 2; yj++)
    5016           0 :             x->hash_value_buffer[xi][yj] = (uint32_t*)malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH * sizeof(uint32_t));
    5017             : 
    5018             :     IntMv nearestmv, nearmv;
    5019           0 :     eb_av1_find_best_ref_mvs_from_stack(0, context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack /*mbmi_ext*/, xd, ref_frame, &nearestmv, &nearmv,
    5020             :         0);
    5021           0 :     if (nearestmv.as_int == INVALID_MV)
    5022           0 :         nearestmv.as_int = 0;
    5023           0 :     if (nearmv.as_int == INVALID_MV)
    5024           0 :         nearmv.as_int = 0;
    5025           0 :     IntMv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
    5026           0 :     if (dv_ref.as_int == 0)
    5027           0 :         av1_find_ref_dv(&dv_ref, tile, scs->seq_header.sb_mi_size, mi_row, mi_col);
    5028             :     // Ref DV should not have sub-pel.
    5029           0 :     assert((dv_ref.as_mv.col & 7) == 0);
    5030           0 :     assert((dv_ref.as_mv.row & 7) == 0);
    5031           0 :     context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[INTRA_FRAME][0].this_mv = dv_ref;
    5032             : 
    5033             :     /* pointer to current frame */
    5034             :     Yv12BufferConfig cur_buf;
    5035           0 :     link_Eb_to_aom_buffer_desc_8bit(
    5036           0 :         pcs->parent_pcs_ptr->enhanced_picture_ptr,
    5037             :         &cur_buf);
    5038             :     struct Buf2D yv12_mb[MAX_MB_PLANE];
    5039           0 :     eb_av1_setup_pred_block(bsize, yv12_mb, &cur_buf, mi_row, mi_col);
    5040           0 :     for (int i = 0; i < num_planes; ++i)
    5041           0 :         x->xdplane[i].pre[0] = yv12_mb[i];  //ref in ME
    5042             :     //setup src for DV search same as ref
    5043           0 :     x->plane[0].src = x->xdplane[0].pre[0];
    5044             : 
    5045             :     enum IntrabcMotionDirection {
    5046             :         IBC_MOTION_ABOVE,
    5047             :         IBC_MOTION_LEFT,
    5048             :         IBC_MOTION_DIRECTIONS
    5049             :     };
    5050             : 
    5051             :     //up to two dv candidates will be generated
    5052           0 :     enum IntrabcMotionDirection max_dir = pcs->parent_pcs_ptr->ibc_mode > 1 ? IBC_MOTION_LEFT : IBC_MOTION_DIRECTIONS;
    5053             : 
    5054           0 :     for (enum IntrabcMotionDirection dir = IBC_MOTION_ABOVE;
    5055           0 :         dir < max_dir; ++dir) {
    5056           0 :         const MvLimits tmp_mv_limits = x->mv_limits;
    5057             : 
    5058           0 :         switch (dir) {
    5059           0 :         case IBC_MOTION_ABOVE:
    5060           0 :             x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE;
    5061           0 :             x->mv_limits.col_max = (tile->mi_col_end - mi_col) * MI_SIZE - w;
    5062           0 :             x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE;
    5063           0 :             x->mv_limits.row_max =
    5064           0 :                 (sb_row * scs->seq_header.sb_mi_size - mi_row) * MI_SIZE - h;
    5065           0 :             break;
    5066           0 :         case IBC_MOTION_LEFT:
    5067           0 :             x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE;
    5068           0 :             x->mv_limits.col_max =
    5069           0 :                 (sb_col * scs->seq_header.sb_mi_size - mi_col) * MI_SIZE - w;
    5070             :             // TODO: Minimize the overlap between above and
    5071             :             // left areas.
    5072           0 :             x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE;
    5073           0 :             int bottom_coded_mi_edge =
    5074           0 :                 AOMMIN((sb_row + 1) * scs->seq_header.sb_mi_size, tile->mi_row_end);
    5075           0 :             x->mv_limits.row_max = (bottom_coded_mi_edge - mi_row) * MI_SIZE - h;
    5076           0 :             break;
    5077           0 :         default: assert(0);
    5078             :         }
    5079           0 :         assert_release(x->mv_limits.col_min >= tmp_mv_limits.col_min);
    5080           0 :         assert_release(x->mv_limits.col_max <= tmp_mv_limits.col_max);
    5081           0 :         assert_release(x->mv_limits.row_min >= tmp_mv_limits.row_min);
    5082           0 :         assert_release(x->mv_limits.row_max <= tmp_mv_limits.row_max);
    5083             : 
    5084           0 :         eb_av1_set_mv_search_range(&x->mv_limits, &dv_ref.as_mv);
    5085             : 
    5086           0 :         if (x->mv_limits.col_max < x->mv_limits.col_min ||
    5087           0 :             x->mv_limits.row_max < x->mv_limits.row_min) {
    5088           0 :             x->mv_limits = tmp_mv_limits;
    5089           0 :             continue;
    5090             :         }
    5091             : 
    5092           0 :         int step_param = 0;
    5093           0 :         MV mvp_full = dv_ref.as_mv;
    5094           0 :         mvp_full.col >>= 3;
    5095           0 :         mvp_full.row >>= 3;
    5096           0 :         const int sadpb = x->sadperbit16;
    5097           0 :         x->best_mv.as_int = 0;
    5098             : 
    5099             : #define INT_VAR_MAX  2147483647    // maximum (signed) int value
    5100             : 
    5101           0 :         const int bestsme = eb_av1_full_pixel_search(
    5102             :             pcs, x, bsize, &mvp_full, step_param, 1, 0,
    5103             :             sadpb, NULL, &dv_ref.as_mv, INT_VAR_MAX, 1,
    5104             :             (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1);
    5105             : 
    5106           0 :         x->mv_limits = tmp_mv_limits;
    5107           0 :         if (bestsme == INT_VAR_MAX) continue;
    5108           0 :         mvp_full = x->best_mv.as_mv;
    5109             : 
    5110           0 :         const MV dv = { .row = mvp_full.row * 8,.col = mvp_full.col * 8 };
    5111           0 :         if (mv_check_bounds(&x->mv_limits, &dv)) continue;
    5112           0 :         if (!av1_is_dv_valid(dv, xd, mi_row, mi_col, bsize,
    5113           0 :             scs->seq_header.sb_size_log2))
    5114           0 :             continue;
    5115             : 
    5116             :         // DV should not have sub-pel.
    5117           0 :         assert_release((dv.col & 7) == 0);
    5118           0 :         assert_release((dv.row & 7) == 0);
    5119             : 
    5120             :         //store output
    5121           0 :         dv_cand[*num_dv_cand] = dv;
    5122           0 :         (*num_dv_cand)++;
    5123             :     }
    5124             : 
    5125           0 :     for (int i = 0; i < 2; i++)
    5126           0 :         for (int j = 0; j < 2; j++)
    5127           0 :             free(x->hash_value_buffer[i][j]);
    5128           0 : }
    5129             : 
    5130           0 : void  inject_intra_bc_candidates(
    5131             :     PictureControlSet            *picture_control_set_ptr,
    5132             :     ModeDecisionContext          *context_ptr,
    5133             :     const SequenceControlSet     *sequence_control_set_ptr,
    5134             :     CodingUnit                   *cu_ptr,
    5135             :     uint32_t                       *cand_cnt)
    5136             : {
    5137             :     MV dv_cand[2];
    5138           0 :     uint8_t num_dv_cand = 0;
    5139             : 
    5140             :     //perform dv-pred + search up to 2 dv(s)
    5141           0 :     intra_bc_search(
    5142             :         picture_control_set_ptr,
    5143             :         context_ptr,
    5144             :         sequence_control_set_ptr,
    5145             :         cu_ptr,
    5146             :         dv_cand,
    5147             :         &num_dv_cand);
    5148             : 
    5149           0 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    5150             :     uint32_t dv_i;
    5151             : 
    5152           0 :     for (dv_i = 0; dv_i < num_dv_cand; dv_i++)
    5153             :     {
    5154             : #if PAL_SUP
    5155           0 :         candidateArray[*cand_cnt].palette_info.pmi.palette_size[0] = 0;
    5156           0 :         candidateArray[*cand_cnt].palette_info.pmi.palette_size[1] = 0;
    5157             : #endif
    5158           0 :         candidateArray[*cand_cnt].type = INTRA_MODE;
    5159           0 :         candidateArray[*cand_cnt].intra_luma_mode = DC_PRED;
    5160           0 :         candidateArray[*cand_cnt].distortion_ready = 0;
    5161           0 :         candidateArray[*cand_cnt].use_intrabc = 1;
    5162           0 :         candidateArray[*cand_cnt].is_directional_mode_flag = 0;
    5163           0 :         candidateArray[*cand_cnt].angle_delta[PLANE_TYPE_Y] = 0;
    5164           0 :         candidateArray[*cand_cnt].intra_chroma_mode = UV_DC_PRED;
    5165           0 :         candidateArray[*cand_cnt].cfl_alpha_signs = 0;
    5166           0 :         candidateArray[*cand_cnt].cfl_alpha_idx = 0;
    5167           0 :         candidateArray[*cand_cnt].is_directional_chroma_mode_flag = 0;
    5168           0 :         candidateArray[*cand_cnt].angle_delta[PLANE_TYPE_UV] = 0;
    5169           0 :         candidateArray[*cand_cnt].transform_type[0] = DCT_DCT;
    5170           0 :         candidateArray[*cand_cnt].transform_type_uv = DCT_DCT;
    5171           0 :         candidateArray[*cand_cnt].ref_frame_type = INTRA_FRAME;
    5172           0 :         candidateArray[*cand_cnt].pred_mode = DC_PRED;
    5173           0 :         candidateArray[*cand_cnt].motion_mode = SIMPLE_TRANSLATION;
    5174             :         //inter ralated
    5175           0 :         candidateArray[*cand_cnt].is_compound = 0;
    5176             : #if II_COMP_FLAG
    5177           0 :         candidateArray[*cand_cnt].is_interintra_used = 0;
    5178             : #endif
    5179           0 :         candidateArray[*cand_cnt].merge_flag = EB_FALSE;
    5180           0 :         candidateArray[*cand_cnt].prediction_direction[0] = UNI_PRED_LIST_0;
    5181           0 :         candidateArray[*cand_cnt].is_new_mv = 0;
    5182           0 :         candidateArray[*cand_cnt].is_zero_mv = 0;
    5183           0 :         candidateArray[*cand_cnt].motion_vector_xl0 = dv_cand[dv_i].col;
    5184           0 :         candidateArray[*cand_cnt].motion_vector_yl0 = dv_cand[dv_i].row;
    5185           0 :         candidateArray[*cand_cnt].motion_vector_pred_x[REF_LIST_0] = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[INTRA_FRAME][0].this_mv.as_mv.col;
    5186           0 :         candidateArray[*cand_cnt].motion_vector_pred_y[REF_LIST_0] = context_ptr->md_local_cu_unit[context_ptr->blk_geom->blkidx_mds].ed_ref_mv_stack[INTRA_FRAME][0].this_mv.as_mv.row;
    5187           0 :         candidateArray[*cand_cnt].drl_index = 0;
    5188           0 :         candidateArray[*cand_cnt].ref_mv_index = 0;
    5189           0 :         candidateArray[*cand_cnt].pred_mv_weight = 0;
    5190           0 :         candidateArray[*cand_cnt].interp_filters = av1_broadcast_interp_filter(BILINEAR);
    5191             : #if FILTER_INTRA_FLAG
    5192           0 :         candidateArray[*cand_cnt].filter_intra_mode = FILTER_INTRA_MODES;
    5193             : #endif
    5194           0 :         INCRMENT_CAND_TOTAL_COUNT( (*cand_cnt) );
    5195             :     }
    5196           0 : }
    5197             : // Indices are sign, integer, and fractional part of the gradient value
    5198             : static const uint8_t gradient_to_angle_bin[2][7][16] = {
    5199             :   {
    5200             :       { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 },
    5201             :       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
    5202             :       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    5203             :       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    5204             :       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    5205             :       { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
    5206             :       { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
    5207             :   },
    5208             :   {
    5209             :       { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 },
    5210             :       { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 },
    5211             :       { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
    5212             :       { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
    5213             :       { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
    5214             :       { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
    5215             :       { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
    5216             :   },
    5217             : };
    5218             : 
    5219             : /* clang-format off */
    5220             : static const uint8_t mode_to_angle_bin[INTRA_MODES] = {
    5221             :   0, 2, 6, 0, 4, 3, 5, 7, 1, 0,
    5222             :   0,
    5223             : };
    5224           0 : void av1_get_gradient_hist_c(const uint8_t *src, int src_stride, int rows,
    5225             :     int cols, uint64_t *hist) {
    5226           0 :     src += src_stride;
    5227           0 :     for (int r = 1; r < rows; ++r) {
    5228           0 :         for (int c = 1; c < cols; ++c) {
    5229           0 :             int dx = src[c] - src[c - 1];
    5230           0 :             int dy = src[c] - src[c - src_stride];
    5231             :             int index;
    5232           0 :             const int temp = dx * dx + dy * dy;
    5233           0 :             if (dy == 0) {
    5234           0 :                 index = 2;
    5235             :             }
    5236             :             else {
    5237           0 :                 const int sn = (dx > 0) ^ (dy > 0);
    5238           0 :                 dx = abs(dx);
    5239           0 :                 dy = abs(dy);
    5240           0 :                 const int remd = (dx % dy) * 16 / dy;
    5241           0 :                 const int quot = dx / dy;
    5242           0 :                 index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)];
    5243             :             }
    5244           0 :             hist[index] += temp;
    5245             :         }
    5246           0 :         src += src_stride;
    5247             :     }
    5248           0 : }
    5249      159298 : static void angle_estimation(
    5250             :     const uint8_t *src,
    5251             :     int src_stride,
    5252             :     int rows,
    5253             :     int cols,
    5254             :     //BLOCK_SIZE bsize,
    5255             :     uint8_t *directional_mode_skip_mask)
    5256             : {
    5257             :     // Check if angle_delta is used
    5258             :     //if (!av1_use_angle_delta(bsize)) return;
    5259             : 
    5260      159298 :     uint64_t hist[DIRECTIONAL_MODES] = { 0 };
    5261             :     //if (is_hbd)
    5262             :     //    get_highbd_gradient_hist(src, src_stride, rows, cols, hist);
    5263             :     //else
    5264      159298 :     av1_get_gradient_hist(src, src_stride, rows, cols, hist);
    5265             : 
    5266             :     int i;
    5267      159369 :     uint64_t hist_sum = 0;
    5268     1434230 :     for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i];
    5269     2228640 :     for (i = 0; i < INTRA_MODES; ++i) {
    5270     2069340 :         if (av1_is_directional_mode(i)) {
    5271     1273910 :             const uint8_t angle_bin = mode_to_angle_bin[i];
    5272     1273910 :             uint64_t score = 2 * hist[angle_bin];
    5273     1273910 :             int weight = 2;
    5274     1273910 :             if (angle_bin > 0) {
    5275     1114720 :                 score += hist[angle_bin - 1];
    5276     1114720 :                 ++weight;
    5277             :             }
    5278     1273910 :             if (angle_bin < DIRECTIONAL_MODES - 1) {
    5279     1114790 :                 score += hist[angle_bin + 1];
    5280     1114790 :                 ++weight;
    5281             :             }
    5282     1273910 :             const int thresh = 10;
    5283     1273910 :             if (score * thresh < hist_sum * weight) directional_mode_skip_mask[i] = 1;
    5284             :         }
    5285             :     }
    5286      159299 : }
    5287             : // END of Function Declarations
    5288      289081 : void  inject_intra_candidates(
    5289             :     PictureControlSet            *picture_control_set_ptr,
    5290             :     ModeDecisionContext          *context_ptr,
    5291             :     const SequenceControlSet     *sequence_control_set_ptr,
    5292             :     LargestCodingUnit            *sb_ptr,
    5293             :     uint32_t                       *candidateTotalCnt){
    5294             :     (void)sequence_control_set_ptr;
    5295             :     (void)sb_ptr;
    5296      289081 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    5297             : #if !PAETH_HBD
    5298             :     uint8_t                     is16bit = (sequence_control_set_ptr->static_config.encoder_bit_depth > EB_8BIT);
    5299             : #endif
    5300      289081 :     uint8_t                     intra_mode_start = DC_PRED;
    5301             : #if PAETH_HBD
    5302      289081 :     uint8_t                     intra_mode_end   =  PAETH_PRED;
    5303             : #else
    5304             :     uint8_t                     intra_mode_end   = is16bit ? SMOOTH_H_PRED : PAETH_PRED;
    5305             : #endif
    5306             :     uint8_t                     openLoopIntraCandidate;
    5307      289081 :     uint32_t                    canTotalCnt = 0;
    5308      289081 :     uint8_t                     angleDeltaCounter = 0;
    5309      289081 :     EbBool                      use_angle_delta = av1_use_angle_delta(context_ptr->blk_geom->bsize);
    5310      289055 :     uint8_t                     angleDeltaCandidateCount = use_angle_delta ? 7 : 1;
    5311      289055 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    5312      289055 :     EbBool                      disable_cfl_flag = (MAX(context_ptr->blk_geom->bheight, context_ptr->blk_geom->bwidth) > 32) ? EB_TRUE : EB_FALSE;
    5313             : 
    5314             :     uint8_t                     disable_z2_prediction;
    5315             :     uint8_t                     disable_angle_refinement;
    5316             :     uint8_t                     disable_angle_prediction;
    5317      289055 :     uint8_t directional_mode_skip_mask[INTRA_MODES] = { 0 };
    5318             : 
    5319      289055 :     if (context_ptr->edge_based_skip_angle_intra && use_angle_delta)
    5320             :     {
    5321      159290 :         EbPictureBufferDesc   *src_pic = picture_control_set_ptr->parent_pcs_ptr->enhanced_picture_ptr;
    5322      159290 :         uint8_t               *src_buf = src_pic->buffer_y + (context_ptr->cu_origin_x + src_pic->origin_x) + (context_ptr->cu_origin_y + src_pic->origin_y) * src_pic->stride_y;
    5323      159290 :         const int rows = block_size_high[context_ptr->blk_geom->bsize];
    5324      159290 :         const int cols = block_size_wide[context_ptr->blk_geom->bsize];
    5325      159290 :         angle_estimation(src_buf, src_pic->stride_y, rows, cols, /*context_ptr->blk_geom->bsize,*/directional_mode_skip_mask);
    5326             :     }
    5327      289111 :     uint8_t     angle_delta_shift = 1;
    5328      289111 :     if (picture_control_set_ptr->parent_pcs_ptr->intra_pred_mode == 4) {
    5329       75594 :         if (picture_control_set_ptr->slice_type == I_SLICE) {
    5330             : #if PAETH_HBD
    5331        5742 :             intra_mode_end =  PAETH_PRED;
    5332             : #else
    5333             :             intra_mode_end = is16bit ? SMOOTH_H_PRED : PAETH_PRED;
    5334             : #endif
    5335        5742 :             angleDeltaCandidateCount = use_angle_delta ? 5 : 1;
    5336        5742 :             disable_angle_prediction = 0;
    5337        5742 :             angle_delta_shift = 2;
    5338        5742 :             disable_z2_prediction = 0;
    5339             :         }
    5340             :         else {
    5341       69852 :             intra_mode_end = DC_PRED;
    5342       69852 :             disable_angle_prediction = 1;
    5343       69852 :             angleDeltaCandidateCount = 1;
    5344       69852 :             angle_delta_shift = 1;
    5345       69852 :             disable_z2_prediction = 0;
    5346             :         }
    5347             :     }else
    5348      213517 :     if (picture_control_set_ptr->parent_pcs_ptr->intra_pred_mode == 3){
    5349           0 :         disable_z2_prediction       = 0;
    5350           0 :         disable_angle_refinement    = 0;
    5351           0 :         disable_angle_prediction    = 1;
    5352           0 :         angleDeltaCandidateCount = disable_angle_refinement ? 1: angleDeltaCandidateCount;
    5353      213517 :     } else if (picture_control_set_ptr->parent_pcs_ptr->intra_pred_mode == 2) {
    5354           0 :         disable_z2_prediction       = 0;
    5355           0 :         disable_angle_refinement    = 0 ;
    5356           0 :         disable_angle_prediction    = (context_ptr->blk_geom->sq_size > 16 ||
    5357           0 :                                        context_ptr->blk_geom->bwidth == 4 ||
    5358           0 :                                        context_ptr->blk_geom->bheight == 4) ? 1 : 0;
    5359           0 :         angleDeltaCandidateCount = disable_angle_refinement ? 1: angleDeltaCandidateCount;
    5360      213517 :     } else if (picture_control_set_ptr->parent_pcs_ptr->intra_pred_mode == 1) {
    5361           0 :         disable_z2_prediction       = (context_ptr->blk_geom->sq_size > 16 ||
    5362           0 :                                        context_ptr->blk_geom->bwidth == 4 ||
    5363           0 :                                        context_ptr->blk_geom->bheight == 4) ? 1 : 0;
    5364           0 :         disable_angle_refinement    = (context_ptr->blk_geom->sq_size > 16 ||
    5365           0 :                                        context_ptr->blk_geom->bwidth == 4 ||
    5366           0 :                                        context_ptr->blk_geom->bheight == 4) ? 1 : 0;
    5367           0 :         disable_angle_prediction    = 0;
    5368           0 :         angleDeltaCandidateCount = disable_angle_refinement ? 1: angleDeltaCandidateCount;
    5369             :     } else {
    5370      213517 :         disable_z2_prediction       = 0;
    5371      213517 :         disable_angle_refinement    = 0;
    5372      213517 :         disable_angle_prediction    = 0;
    5373      213517 :         angleDeltaCandidateCount = disable_angle_refinement ? 1: angleDeltaCandidateCount;
    5374             :     }
    5375             : #if MR_MODE
    5376             :     disable_z2_prediction       = 0;
    5377             :     disable_angle_refinement    = 0;
    5378             :     disable_angle_prediction    = 0;
    5379             : #endif
    5380     3209050 :     for (openLoopIntraCandidate = intra_mode_start; openLoopIntraCandidate <= intra_mode_end ; ++openLoopIntraCandidate) {
    5381     2919870 :         if (av1_is_directional_mode((PredictionMode)openLoopIntraCandidate)) {
    5382     1753960 :             if (!disable_angle_prediction &&
    5383     1753960 :                 directional_mode_skip_mask[(PredictionMode)openLoopIntraCandidate] == 0) {
    5384     8379360 :                 for (angleDeltaCounter = 0; angleDeltaCounter < angleDeltaCandidateCount; ++angleDeltaCounter) {
    5385     6932170 :                     int32_t angle_delta = CLIP( angle_delta_shift * (angleDeltaCandidateCount == 1 ? 0 : angleDeltaCounter - (angleDeltaCandidateCount >> 1)), -3 , 3);
    5386     6932170 :                     int32_t  p_angle = mode_to_angle_map[(PredictionMode)openLoopIntraCandidate] + angle_delta * ANGLE_STEP;
    5387     6932170 :                     if (!disable_z2_prediction || (p_angle <= 90 || p_angle >= 180)) {
    5388     6932170 :                         candidateArray[canTotalCnt].type = INTRA_MODE;
    5389             : #if PAL_SUP
    5390     6932170 :                         candidateArray[canTotalCnt].palette_info.pmi.palette_size[0] = 0;
    5391     6932170 :                         candidateArray[canTotalCnt].palette_info.pmi.palette_size[1] = 0;
    5392             : #endif
    5393     6932170 :                         candidateArray[canTotalCnt].intra_luma_mode = openLoopIntraCandidate;
    5394     6932170 :                         candidateArray[canTotalCnt].distortion_ready = 0;
    5395     6932170 :                         candidateArray[canTotalCnt].use_intrabc = 0;
    5396             : #if FILTER_INTRA_FLAG
    5397     6932170 :                         candidateArray[canTotalCnt].filter_intra_mode = FILTER_INTRA_MODES;
    5398             : #endif
    5399     6932170 :                         candidateArray[canTotalCnt].is_directional_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)openLoopIntraCandidate);
    5400     6931970 :                         candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y] = angle_delta;
    5401             :                         // Search the best independent intra chroma mode
    5402     6931970 :                         if (context_ptr->chroma_level == CHROMA_MODE_0) {
    5403     3965230 :                             candidateArray[canTotalCnt].intra_chroma_mode = disable_cfl_flag ?
    5404     3965230 :                                 context_ptr->best_uv_mode[openLoopIntraCandidate][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] :
    5405             :                                 UV_CFL_PRED ;
    5406     3965230 :                             candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = disable_cfl_flag ?
    5407     3965230 :                                 context_ptr->best_uv_angle[candidateArray[canTotalCnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] : 0;
    5408     4144310 :                             candidateArray[canTotalCnt].is_directional_chroma_mode_flag = disable_cfl_flag ?
    5409      179083 :                                 (uint8_t)av1_is_directional_mode((PredictionMode)(context_ptr->best_uv_mode[candidateArray[canTotalCnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]])) : 0;
    5410             :                         }
    5411             :                         else {
    5412             :                             // Hsan/Omar: why the restriction below ? (i.e. disable_ang_uv)
    5413     2966740 :                             const int32_t disable_ang_uv = (context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) && context_ptr->blk_geom->has_uv ? 1 : 0;
    5414     2966740 :                             candidateArray[canTotalCnt].intra_chroma_mode = disable_cfl_flag ?
    5415     5636200 :                                 intra_luma_to_chroma[openLoopIntraCandidate] :
    5416     2669460 :                                 (context_ptr->chroma_level == CHROMA_MODE_1) ?
    5417     2669460 :                                 UV_CFL_PRED :
    5418             :                                 UV_DC_PRED;
    5419      134519 :                             candidateArray[canTotalCnt].intra_chroma_mode = disable_ang_uv && av1_is_directional_mode(candidateArray[canTotalCnt].intra_chroma_mode) ?
    5420     3101260 :                                 UV_DC_PRED : candidateArray[canTotalCnt].intra_chroma_mode;
    5421     2966740 :                             candidateArray[canTotalCnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidateArray[canTotalCnt].intra_chroma_mode);
    5422     2967100 :                             candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = 0;
    5423             :                         }
    5424     6932320 :                         candidateArray[canTotalCnt].cfl_alpha_signs = 0;
    5425     6932320 :                         candidateArray[canTotalCnt].cfl_alpha_idx = 0;
    5426     6932320 :                         candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    5427             : 
    5428     6932320 :                         if (candidateArray[canTotalCnt].intra_chroma_mode == UV_CFL_PRED)
    5429     6330760 :                             candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    5430             :                         else
    5431      601437 :                             candidateArray[canTotalCnt].transform_type_uv =
    5432      601566 :                             av1_get_tx_type(
    5433      601566 :                                 context_ptr->blk_geom->bsize,
    5434             :                                 0,
    5435      601566 :                                 (PredictionMode)candidateArray[canTotalCnt].intra_luma_mode,
    5436      601566 :                                 (UvPredictionMode)candidateArray[canTotalCnt].intra_chroma_mode,
    5437             :                                 PLANE_TYPE_UV,
    5438             :                                 0,
    5439             :                                 0,
    5440             :                                 0,
    5441      601566 :                                 context_ptr->blk_geom->txsize_uv[0][0],
    5442      601566 :                                 frm_hdr->reduced_tx_set);
    5443     6932200 :                         candidateArray[canTotalCnt].ref_frame_type = INTRA_FRAME;
    5444     6932200 :                         candidateArray[canTotalCnt].pred_mode = (PredictionMode)openLoopIntraCandidate;
    5445     6932200 :                         candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    5446     6932200 :                         INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    5447             :                     }
    5448             :             }
    5449             :         }
    5450             :         }
    5451             :         else {
    5452     1165890 :             candidateArray[canTotalCnt].type = INTRA_MODE;
    5453             : #if PAL_SUP
    5454     1165890 :             candidateArray[canTotalCnt].palette_info.pmi.palette_size[0] = 0;
    5455     1165890 :             candidateArray[canTotalCnt].palette_info.pmi.palette_size[1] = 0;
    5456             : #endif
    5457     1165890 :             candidateArray[canTotalCnt].intra_luma_mode = openLoopIntraCandidate;
    5458     1165890 :             candidateArray[canTotalCnt].distortion_ready = 0;
    5459     1165890 :             candidateArray[canTotalCnt].use_intrabc = 0;
    5460             : #if FILTER_INTRA_FLAG
    5461     1165890 :             candidateArray[canTotalCnt].filter_intra_mode = FILTER_INTRA_MODES;
    5462             : #endif
    5463     1165890 :             candidateArray[canTotalCnt].is_directional_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)openLoopIntraCandidate);
    5464     1166090 :             candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y] = 0;
    5465             :             // Search the best independent intra chroma mode
    5466     1166090 :             if (context_ptr->chroma_level == CHROMA_MODE_0) {
    5467      551397 :                 candidateArray[canTotalCnt].intra_chroma_mode = disable_cfl_flag ?
    5468      551397 :                     context_ptr->best_uv_mode[openLoopIntraCandidate][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] :
    5469             :                     UV_CFL_PRED;
    5470      551397 :                 candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = disable_cfl_flag ?
    5471      551397 :                     context_ptr->best_uv_angle[candidateArray[canTotalCnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] : 0;
    5472      567387 :                 candidateArray[canTotalCnt].is_directional_chroma_mode_flag = disable_cfl_flag ?
    5473       15990 :                     (uint8_t)av1_is_directional_mode((PredictionMode)(context_ptr->best_uv_mode[candidateArray[canTotalCnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]])) : 0;
    5474             :             }
    5475             :             else {
    5476             :                 // Hsan/Omar: why the restriction below ? (i.e. disable_ang_uv)
    5477      614693 :                 const int32_t disable_ang_uv = (context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) && context_ptr->blk_geom->has_uv ? 1 : 0;
    5478      614693 :                 candidateArray[canTotalCnt].intra_chroma_mode = disable_cfl_flag ?
    5479     1179560 :                     intra_luma_to_chroma[openLoopIntraCandidate] :
    5480      564866 :                     (context_ptr->chroma_level == CHROMA_MODE_1) ?
    5481      564866 :                         UV_CFL_PRED :
    5482             :                         UV_DC_PRED;
    5483             : 
    5484       31050 :                 candidateArray[canTotalCnt].intra_chroma_mode = disable_ang_uv && av1_is_directional_mode(candidateArray[canTotalCnt].intra_chroma_mode) ?
    5485      645743 :                     UV_DC_PRED : candidateArray[canTotalCnt].intra_chroma_mode;
    5486             : 
    5487      614693 :                 candidateArray[canTotalCnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidateArray[canTotalCnt].intra_chroma_mode);
    5488      614728 :                 candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = 0;
    5489             : 
    5490             :             }
    5491     1166120 :             candidateArray[canTotalCnt].cfl_alpha_signs = 0;
    5492     1166120 :             candidateArray[canTotalCnt].cfl_alpha_idx = 0;
    5493     1166120 :             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    5494             : 
    5495     1166120 :             if (candidateArray[canTotalCnt].intra_chroma_mode == UV_CFL_PRED)
    5496     1004980 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    5497             :             else
    5498      161189 :                 candidateArray[canTotalCnt].transform_type_uv =
    5499      161144 :                 av1_get_tx_type(
    5500      161144 :                     context_ptr->blk_geom->bsize,
    5501             :                     0,
    5502      161144 :                     (PredictionMode)candidateArray[canTotalCnt].intra_luma_mode,
    5503      161144 :                     (UvPredictionMode)candidateArray[canTotalCnt].intra_chroma_mode,
    5504             :                     PLANE_TYPE_UV,
    5505             :                     0,
    5506             :                     0,
    5507             :                     0,
    5508      161144 :                     context_ptr->blk_geom->txsize_uv[0][0],
    5509      161144 :                     frm_hdr->reduced_tx_set);
    5510     1166170 :             candidateArray[canTotalCnt].ref_frame_type = INTRA_FRAME;
    5511     1166170 :             candidateArray[canTotalCnt].pred_mode = (PredictionMode)openLoopIntraCandidate;
    5512     1166170 :             candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    5513     1166170 :             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    5514             :         }
    5515             :     }
    5516             : 
    5517             :     // update the total number of candidates injected
    5518      289179 :     (*candidateTotalCnt) = canTotalCnt;
    5519             : 
    5520      289179 :     return;
    5521             : }
    5522             : 
    5523             : #if FILTER_INTRA_FLAG
    5524             : // END of Function Declarations
    5525      120485 : void  inject_filter_intra_candidates(
    5526             :     PictureControlSet            *picture_control_set_ptr,
    5527             :     ModeDecisionContext          *context_ptr,
    5528             :     uint32_t                     *candidateTotalCnt){
    5529             : 
    5530      120485 :     FilterIntraMode             intra_mode_start = FILTER_DC_PRED;
    5531      120485 :     FilterIntraMode             intra_mode_end   = FILTER_INTRA_MODES;
    5532             :     FilterIntraMode             filter_intra_mode;
    5533      120485 :     uint32_t                    canTotalCnt = *candidateTotalCnt;
    5534      120485 :     ModeDecisionCandidate      *candidateArray = context_ptr->fast_candidate_array;
    5535             : 
    5536      120485 :     EbBool                      disable_cfl_flag = (MAX(context_ptr->blk_geom->bheight, context_ptr->blk_geom->bwidth) > 32) ? EB_TRUE : EB_FALSE;
    5537             : 
    5538      120485 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    5539             : 
    5540      722891 :     for (filter_intra_mode = intra_mode_start; filter_intra_mode < intra_mode_end ; ++filter_intra_mode) {
    5541             : 
    5542      602407 :             candidateArray[canTotalCnt].type = INTRA_MODE;
    5543      602407 :             candidateArray[canTotalCnt].intra_luma_mode = DC_PRED;
    5544      602407 :             candidateArray[canTotalCnt].distortion_ready = 0;
    5545      602407 :             candidateArray[canTotalCnt].use_intrabc = 0;
    5546      602407 :             candidateArray[canTotalCnt].filter_intra_mode = filter_intra_mode;
    5547      602407 :             candidateArray[canTotalCnt].is_directional_mode_flag = 0;
    5548             : 
    5549             : #if PAL_SUP
    5550      602407 :             candidateArray[canTotalCnt].palette_info.pmi.palette_size[0] = 0;
    5551      602407 :             candidateArray[canTotalCnt].palette_info.pmi.palette_size[1] = 0;
    5552             : #endif
    5553             : 
    5554      602407 :             candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y] = 0;
    5555             : 
    5556             :             // Search the best independent intra chroma mode
    5557      602407 :             if (context_ptr->chroma_level == CHROMA_MODE_0) {
    5558      602407 :                 candidateArray[canTotalCnt].intra_chroma_mode  = disable_cfl_flag ? context_ptr->best_uv_mode[fimode_to_intramode[filter_intra_mode]][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] : UV_CFL_PRED;
    5559             : 
    5560      602407 :                 candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = disable_cfl_flag ? context_ptr->best_uv_angle[fimode_to_intramode[filter_intra_mode]][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]] : 0;
    5561      602407 :                 candidateArray[canTotalCnt].is_directional_chroma_mode_flag = disable_cfl_flag ? (uint8_t)av1_is_directional_mode((PredictionMode)(context_ptr->best_uv_mode[fimode_to_intramode[filter_intra_mode]][MAX_ANGLE_DELTA + candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_Y]])) : 0;
    5562             : 
    5563             :             }
    5564             :             else {
    5565             :                 // Hsan/Omar: why the restriction below ? (i.e. disable_ang_uv)
    5566           0 :                 const int32_t disable_ang_uv = (context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) && context_ptr->blk_geom->has_uv ? 1 : 0;
    5567           0 :                 candidateArray[canTotalCnt].intra_chroma_mode = disable_cfl_flag ? intra_luma_to_chroma[fimode_to_intramode[filter_intra_mode]] :
    5568           0 :                     (context_ptr->chroma_level == CHROMA_MODE_1) ?
    5569           0 :                         UV_CFL_PRED :
    5570             :                         UV_DC_PRED;
    5571             : 
    5572           0 :                 candidateArray[canTotalCnt].intra_chroma_mode =  disable_ang_uv && av1_is_directional_mode(candidateArray[canTotalCnt].intra_chroma_mode) ?
    5573           0 :                     UV_DC_PRED : candidateArray[canTotalCnt].intra_chroma_mode;
    5574             : 
    5575           0 :                 candidateArray[canTotalCnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidateArray[canTotalCnt].intra_chroma_mode);
    5576           0 :                 candidateArray[canTotalCnt].angle_delta[PLANE_TYPE_UV] = 0;
    5577             :             }
    5578             : 
    5579      602407 :             candidateArray[canTotalCnt].cfl_alpha_signs = 0;
    5580      602407 :             candidateArray[canTotalCnt].cfl_alpha_idx = 0;
    5581      602407 :             candidateArray[canTotalCnt].transform_type[0] = DCT_DCT;
    5582             : 
    5583      602407 :             if (candidateArray[canTotalCnt].intra_chroma_mode == UV_CFL_PRED)
    5584      602405 :                 candidateArray[canTotalCnt].transform_type_uv = DCT_DCT;
    5585             :             else
    5586           0 :                 candidateArray[canTotalCnt].transform_type_uv =
    5587           2 :                 av1_get_tx_type(
    5588           2 :                     context_ptr->blk_geom->bsize,
    5589             :                     0,
    5590           2 :                     (PredictionMode)candidateArray[canTotalCnt].intra_luma_mode,
    5591           2 :                     (UvPredictionMode)candidateArray[canTotalCnt].intra_chroma_mode,
    5592             :                     PLANE_TYPE_UV,
    5593             :                     0,
    5594             :                     0,
    5595             :                     0,
    5596           2 :                     context_ptr->blk_geom->txsize_uv[0][0],
    5597           2 :                     frm_hdr->reduced_tx_set);
    5598             : 
    5599      602405 :             candidateArray[canTotalCnt].ref_frame_type = INTRA_FRAME;
    5600      602405 :             candidateArray[canTotalCnt].pred_mode = DC_PRED;
    5601      602405 :             candidateArray[canTotalCnt].motion_mode = SIMPLE_TRANSLATION;
    5602             : 
    5603      602405 :             INCRMENT_CAND_TOTAL_COUNT(canTotalCnt);
    5604             :     }
    5605             : 
    5606             :     // update the total number of candidates injected
    5607      120484 :     (*candidateTotalCnt) = canTotalCnt;
    5608             : 
    5609      120484 :     return;
    5610             : }
    5611             : #endif
    5612             : #if PAL_SUP
    5613     1057500 : int svt_av1_allow_palette(int allow_palette,
    5614             :     BlockSize sb_type) {
    5615     1057500 :     assert(sb_type < BlockSizeS_ALL);
    5616           0 :     return allow_palette && block_size_wide[sb_type] <= 64 &&
    5617     1057500 :         block_size_high[sb_type] <= 64 && sb_type >= BLOCK_8X8;
    5618             : }
    5619             : void  search_palette_luma(
    5620             :     PictureControlSet            *picture_control_set_ptr,
    5621             :     ModeDecisionContext          *context_ptr,
    5622             :     PaletteInfo                 *palette_cand,
    5623             :     uint32_t                     *tot_palette_cands);
    5624             : 
    5625           0 : void  inject_palette_candidates(
    5626             :     PictureControlSet            *picture_control_set_ptr,
    5627             :     ModeDecisionContext          *context_ptr,
    5628             :     uint32_t                       *candidate_total_cnt) {
    5629             : 
    5630             : 
    5631             : 
    5632           0 :     uint32_t                  can_total_cnt = *candidate_total_cnt;
    5633           0 :     ModeDecisionCandidate    *candidateArray = context_ptr->fast_candidate_array;
    5634           0 :     EbBool                    disable_cfl_flag = (MAX(context_ptr->blk_geom->bheight, context_ptr->blk_geom->bwidth) > 32) ? EB_TRUE : EB_FALSE;
    5635             :     uint32_t cand_i;
    5636           0 :     uint32_t tot_palette_cands = 0;
    5637           0 :     PaletteInfo    *palette_cand_array = context_ptr->palette_cand_array;
    5638             : 
    5639           0 :     search_palette_luma(
    5640             :         picture_control_set_ptr,
    5641             :         context_ptr,
    5642             :         palette_cand_array,
    5643             :         &tot_palette_cands);
    5644             : 
    5645           0 :     for (cand_i = 0; cand_i < tot_palette_cands; ++cand_i) {
    5646             : 
    5647           0 :         palette_cand_array[cand_i].pmi.palette_size[1] = 0;
    5648           0 :         memcpy(candidateArray[can_total_cnt].palette_info.color_idx_map, palette_cand_array[cand_i].color_idx_map, 64 * 64);
    5649           0 :         memcpy(&candidateArray[can_total_cnt].palette_info.pmi, &palette_cand_array[cand_i].pmi, sizeof(PaletteModeInfo));
    5650           0 :         assert(palette_cand_array[cand_i].pmi.palette_size[0] < 9);
    5651             :         //to re check these fields
    5652           0 :         candidateArray[can_total_cnt].type = INTRA_MODE;
    5653           0 :         candidateArray[can_total_cnt].intra_luma_mode = DC_PRED;
    5654           0 :         candidateArray[can_total_cnt].distortion_ready = 0;
    5655           0 :         candidateArray[can_total_cnt].use_intrabc = 0;
    5656             : 
    5657           0 :         candidateArray[can_total_cnt].filter_intra_mode = FILTER_INTRA_MODES;
    5658             : 
    5659           0 :         candidateArray[can_total_cnt].is_directional_mode_flag = 0;
    5660             : 
    5661           0 :         candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_Y] = 0;
    5662             : 
    5663             :         // Search the best independent intra chroma mode
    5664           0 :         if (context_ptr->chroma_level == CHROMA_MODE_0) {
    5665           0 :             candidateArray[can_total_cnt].intra_chroma_mode = disable_cfl_flag ?
    5666           0 :                 context_ptr->best_uv_mode[DC_PRED][MAX_ANGLE_DELTA + candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_Y]] :
    5667             :                 UV_CFL_PRED;
    5668             : 
    5669           0 :             candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_UV] = disable_cfl_flag ?
    5670           0 :                 context_ptr->best_uv_angle[candidateArray[can_total_cnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_Y]] : 0;
    5671           0 :             candidateArray[can_total_cnt].is_directional_chroma_mode_flag = disable_cfl_flag ?
    5672           0 :                 (uint8_t)av1_is_directional_mode((PredictionMode)(context_ptr->best_uv_mode[candidateArray[can_total_cnt].intra_luma_mode][MAX_ANGLE_DELTA + candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_Y]])) : 0;
    5673             : 
    5674             :         }
    5675             :         else {
    5676             :             // Hsan/Omar: why the restriction below ? (i.e. disable_ang_uv)
    5677           0 :             const int32_t disable_ang_uv = (context_ptr->blk_geom->bwidth == 4 || context_ptr->blk_geom->bheight == 4) && context_ptr->blk_geom->has_uv ? 1 : 0;
    5678           0 :             candidateArray[can_total_cnt].intra_chroma_mode = disable_cfl_flag ?
    5679           0 :                 intra_luma_to_chroma[DC_PRED] :
    5680           0 :                 (context_ptr->chroma_level == CHROMA_MODE_1) ?
    5681           0 :                 UV_CFL_PRED :
    5682             :                 UV_DC_PRED;
    5683             : 
    5684           0 :             candidateArray[can_total_cnt].intra_chroma_mode = disable_ang_uv && av1_is_directional_mode(candidateArray[can_total_cnt].intra_chroma_mode) ?
    5685           0 :                 UV_DC_PRED : candidateArray[can_total_cnt].intra_chroma_mode;
    5686             : 
    5687           0 :             candidateArray[can_total_cnt].is_directional_chroma_mode_flag = (uint8_t)av1_is_directional_mode((PredictionMode)candidateArray[can_total_cnt].intra_chroma_mode);
    5688           0 :             candidateArray[can_total_cnt].angle_delta[PLANE_TYPE_UV] = 0;
    5689             : 
    5690             :         }
    5691             : 
    5692           0 :         candidateArray[can_total_cnt].cfl_alpha_signs = 0;
    5693           0 :         candidateArray[can_total_cnt].cfl_alpha_idx = 0;
    5694           0 :         candidateArray[can_total_cnt].transform_type[0] = DCT_DCT;
    5695             : 
    5696           0 :         if (candidateArray[can_total_cnt].intra_chroma_mode == UV_CFL_PRED)
    5697           0 :             candidateArray[can_total_cnt].transform_type_uv = DCT_DCT;
    5698             :         else
    5699           0 :             candidateArray[can_total_cnt].transform_type_uv =
    5700             : 
    5701           0 :             av1_get_tx_type(
    5702           0 :                 context_ptr->blk_geom->bsize,
    5703             :                 0,
    5704           0 :                 (PredictionMode)candidateArray[can_total_cnt].intra_luma_mode,
    5705           0 :                 (UvPredictionMode)candidateArray[can_total_cnt].intra_chroma_mode,
    5706             :                 PLANE_TYPE_UV,
    5707             :                 0,
    5708             :                 0,
    5709             :                 0,
    5710           0 :                 context_ptr->blk_geom->txsize_uv[0][0],
    5711           0 :                 picture_control_set_ptr->parent_pcs_ptr->frm_hdr.reduced_tx_set);
    5712             : 
    5713           0 :         candidateArray[can_total_cnt].ref_frame_type = INTRA_FRAME;
    5714           0 :         candidateArray[can_total_cnt].pred_mode = (PredictionMode)DC_PRED;
    5715           0 :         candidateArray[can_total_cnt].motion_mode = SIMPLE_TRANSLATION;
    5716           0 :         INCRMENT_CAND_TOTAL_COUNT(can_total_cnt);
    5717             :     }
    5718             : 
    5719             :     // update the total number of candidates injected
    5720           0 :     (*candidate_total_cnt) = can_total_cnt;
    5721             : 
    5722           0 :     return;
    5723             : }
    5724             : #endif
    5725      811325 : EbErrorType generate_md_stage_0_cand(
    5726             :     LargestCodingUnit   *sb_ptr,
    5727             :     ModeDecisionContext *context_ptr,
    5728             :     SsMeContext         *ss_mecontext,
    5729             :     uint32_t            *candidate_total_count_ptr,
    5730             :     PictureControlSet   *picture_control_set_ptr)
    5731             : {
    5732             : 
    5733      811325 :     FrameHeader *frm_hdr = &picture_control_set_ptr->parent_pcs_ptr->frm_hdr;
    5734      811325 :     const SequenceControlSet *sequence_control_set_ptr = (SequenceControlSet*)picture_control_set_ptr->sequence_control_set_wrapper_ptr->object_ptr;
    5735      811325 :     const EB_SLICE slice_type = picture_control_set_ptr->slice_type;
    5736      811325 :     uint32_t canTotalCnt = 0;
    5737             :     // Reset duplicates variables
    5738      811325 :     context_ptr->injected_mv_count_l0 = 0;
    5739      811325 :     context_ptr->injected_mv_count_l1 = 0;
    5740      811325 :     context_ptr->injected_mv_count_bipred = 0;
    5741      811325 :     uint8_t sq_index = LOG2F(context_ptr->blk_geom->sq_size) - 2;
    5742      811319 :     uint8_t inject_intra_candidate = 1;
    5743      811319 :     uint8_t inject_inter_candidate = 1;
    5744             : 
    5745      811319 :     if (slice_type != I_SLICE) {
    5746      745089 :         if (picture_control_set_ptr->parent_pcs_ptr->nsq_search_level >= NSQ_SEARCH_LEVEL1 &&
    5747      675245 :             picture_control_set_ptr->parent_pcs_ptr->nsq_search_level < NSQ_SEARCH_FULL) {
    5748      675246 :             if (context_ptr->md_local_cu_unit[context_ptr->blk_geom->sqi_mds].avail_blk_flag)
    5749     1193070 :                 inject_intra_candidate = context_ptr->blk_geom->shape == PART_N ? 1 :
    5750      596536 :                     context_ptr->parent_sq_has_coeff[sq_index] != 0 ? inject_intra_candidate : 0;
    5751             :         }
    5752             : }
    5753             :     //----------------------
    5754             :     // Intra
    5755      811319 :     if (context_ptr->blk_geom->sq_size < 128) {
    5756      811351 :         if (picture_control_set_ptr->parent_pcs_ptr->intra_pred_mode >= 5 && context_ptr->blk_geom->sq_size > 4 && context_ptr->blk_geom->shape == PART_N)
    5757           0 :             inject_intra_candidates_ois(
    5758             :                 picture_control_set_ptr,
    5759             :                 context_ptr,
    5760             :                 sb_ptr,
    5761             :                 &canTotalCnt);
    5762             :         else
    5763      811351 :             if (inject_intra_candidate)
    5764      289122 :             inject_intra_candidates(
    5765             :                 picture_control_set_ptr,
    5766             :                 context_ptr,
    5767             :                 sequence_control_set_ptr,
    5768             :                 sb_ptr,
    5769             :                 &canTotalCnt);
    5770             :     }
    5771             : #if FILTER_INTRA_FLAG
    5772      811356 :        if (picture_control_set_ptr->pic_filter_intra_mode > 0 && av1_filter_intra_allowed_bsize(sequence_control_set_ptr->seq_header.enable_filter_intra, context_ptr->blk_geom->bsize))
    5773      120485 :             inject_filter_intra_candidates(
    5774             :                 picture_control_set_ptr,
    5775             :                 context_ptr,
    5776             :                 &canTotalCnt);
    5777             : 
    5778             : #endif
    5779      811355 :     if (frm_hdr->allow_intrabc)
    5780           0 :         inject_intra_bc_candidates(
    5781             :             picture_control_set_ptr,
    5782             :             context_ptr,
    5783             :             sequence_control_set_ptr,
    5784             :             context_ptr->cu_ptr,
    5785             :             &canTotalCnt
    5786             :         );
    5787             : 
    5788             : #if PAL_SUP
    5789             :     //can be removed later if need be
    5790     9513620 :     for (uint16_t i = 0; i < canTotalCnt; i++) {
    5791     8702230 :         assert(context_ptr->fast_candidate_array[i].palette_info.pmi.palette_size[0] == 0);
    5792     8702230 :         assert(context_ptr->fast_candidate_array[i].palette_info.pmi.palette_size[1] == 0);
    5793             :     }
    5794      811391 :     if (svt_av1_allow_palette(picture_control_set_ptr->parent_pcs_ptr->palette_mode, context_ptr->blk_geom->bsize)) {
    5795           0 :         inject_palette_candidates(
    5796             :             picture_control_set_ptr,
    5797             :             context_ptr,
    5798             :             &canTotalCnt);
    5799             :     }
    5800     9513320 :     for (uint16_t i = 0; i < canTotalCnt; i++) {
    5801     8701970 :         assert(context_ptr->fast_candidate_array[i].palette_info.pmi.palette_size[0] < 9);
    5802     8701970 :         assert(context_ptr->fast_candidate_array[i].palette_info.pmi.palette_size[1] == 0);
    5803             :     }
    5804             : #endif
    5805             : 
    5806             :     // Track the total number of fast intra candidates
    5807      811347 :     context_ptr->fast_candidate_intra_count = canTotalCnt;
    5808             : 
    5809      811347 :     if (slice_type != I_SLICE) {
    5810      745097 :         if (inject_inter_candidate)
    5811      745105 :             inject_inter_candidates(
    5812             :                 picture_control_set_ptr,
    5813             :                 context_ptr,
    5814             :                 ss_mecontext,
    5815             :                 sequence_control_set_ptr,
    5816             :                 sb_ptr,
    5817             :                 &canTotalCnt);
    5818             :     }
    5819             : 
    5820      811244 :     *candidate_total_count_ptr = canTotalCnt;
    5821             :     CAND_CLASS  cand_class_it;
    5822      811244 :     memset(context_ptr->md_stage_0_count, 0, CAND_CLASS_TOTAL * sizeof(uint32_t));
    5823             : 
    5824             :     uint32_t cand_i;
    5825   112335000 :     for (cand_i = 0; cand_i < canTotalCnt; cand_i++)
    5826             :     {
    5827   111523000 :         ModeDecisionCandidate * cand_ptr = &context_ptr->fast_candidate_array[cand_i];
    5828             : 
    5829   111523000 :         if (cand_ptr->type == INTRA_MODE) {
    5830             :             // Intra prediction
    5831             : #if FILTER_INTRA_FLAG
    5832     8701780 :                 if (cand_ptr->filter_intra_mode == FILTER_INTRA_MODES) {
    5833             : #if PAL_CLASS
    5834     8099470 :                   if (cand_ptr->palette_info.pmi.palette_size[0] == 0) {
    5835             : #endif
    5836     8099490 :                     cand_ptr->cand_class = CAND_CLASS_0;
    5837     8099490 :                     context_ptr->md_stage_0_count[CAND_CLASS_0]++;
    5838             : #if PAL_CLASS
    5839             :                   }
    5840             :                   else {
    5841           0 :                      cand_ptr->cand_class = CAND_CLASS_7;
    5842           0 :                      context_ptr->md_stage_0_count[CAND_CLASS_7]++;
    5843             :                   }
    5844             : #endif
    5845             :                 }
    5846             :                 else {
    5847      602308 :                     cand_ptr->cand_class = CAND_CLASS_6;
    5848      602308 :                     context_ptr->md_stage_0_count[CAND_CLASS_6]++;
    5849             :                 }
    5850             : #else
    5851             :             cand_ptr->cand_class = CAND_CLASS_0;
    5852             :             context_ptr->md_stage_0_count[CAND_CLASS_0]++;
    5853             : #endif
    5854   102821000 :         } else if (cand_ptr->inter_mode == GLOBALMV || cand_ptr->inter_mode == GLOBAL_GLOBALMV) {
    5855     1755170 :             cand_ptr->cand_class = CAND_CLASS_8;
    5856     1755170 :             context_ptr->md_stage_0_count[CAND_CLASS_8]++;
    5857             :         }
    5858             : #if OBMC_FLAG
    5859             :         else {
    5860             :             // Inter pred
    5861   101066000 :             if (cand_ptr->motion_mode == OBMC_CAUSAL) {
    5862             :                 // OBMC
    5863     7109440 :                 cand_ptr->cand_class = CAND_CLASS_5;
    5864     7109440 :                 context_ptr->md_stage_0_count[CAND_CLASS_5]++;
    5865             :             }
    5866    93956900 :             else if (cand_ptr->is_compound == 0 ||
    5867    71299700 :                     (cand_ptr->is_compound == 1 && cand_ptr->interinter_comp.type == COMPOUND_AVERAGE)) {
    5868             : #else
    5869             :             else if ((cand_ptr->type == INTER_MODE && cand_ptr->is_compound == 0) ||
    5870             :                 (cand_ptr->type == INTER_MODE && cand_ptr->is_compound == 1 && cand_ptr->interinter_comp.type == COMPOUND_AVERAGE)) {
    5871             : #endif
    5872             : #if II_COMP_FLAG
    5873    42741600 :                 if (cand_ptr->is_interintra_used && cand_ptr->is_compound == 0) {
    5874             :                     // InterIntra
    5875     8314780 :                     cand_ptr->cand_class = CAND_CLASS_4;
    5876     8314780 :                     context_ptr->md_stage_0_count[CAND_CLASS_4]++;
    5877             : 
    5878             :                 }
    5879             :                 else
    5880             : #endif
    5881    34426800 :                 if (context_ptr->combine_class12) {
    5882      621174 :                     cand_ptr->cand_class = CAND_CLASS_1;
    5883      621174 :                     context_ptr->md_stage_0_count[CAND_CLASS_1]++;
    5884             :                 }
    5885             :                 else {
    5886    33805600 :                     if (cand_ptr->is_new_mv) {
    5887             :                         // ME pred
    5888    10690600 :                         cand_ptr->cand_class = CAND_CLASS_1;
    5889    10690600 :                         context_ptr->md_stage_0_count[CAND_CLASS_1]++;
    5890             :                     }
    5891             :                     else {
    5892             :                         // MV pred
    5893    23115000 :                         cand_ptr->cand_class = CAND_CLASS_2;
    5894    23115000 :                         context_ptr->md_stage_0_count[CAND_CLASS_2]++;
    5895             :                     }
    5896             :                 }
    5897             :             }
    5898             :             else {
    5899             : #if !OBMC_FLAG
    5900             :                 if (context_ptr->combine_class12) {
    5901             :                     cand_ptr->cand_class = CAND_CLASS_2;
    5902             :                     context_ptr->md_stage_0_count[CAND_CLASS_2]++;
    5903             :                 }
    5904             :                 else {
    5905             : #endif
    5906             :                 // InterInter
    5907    51215300 :                 cand_ptr->cand_class = CAND_CLASS_3;
    5908    51215300 :                 context_ptr->md_stage_0_count[CAND_CLASS_3]++;
    5909             : #if !OBMC_FLAG
    5910             :                 }
    5911             : #endif
    5912             :             }
    5913             : #if OBMC_FLAG
    5914             :         }
    5915             : #endif
    5916             :     }
    5917      811244 :     uint32_t fast_accum = 0;
    5918     8113320 :     for (cand_class_it = CAND_CLASS_0; cand_class_it < CAND_CLASS_TOTAL; cand_class_it++) {
    5919     7302080 :         fast_accum += context_ptr->md_stage_0_count[cand_class_it];
    5920             :     }
    5921      811244 :     assert(fast_accum == canTotalCnt);
    5922             : 
    5923      811244 :     return EB_ErrorNone;
    5924             : }
    5925             : 
    5926             : /***************************************
    5927             : * Full Mode Decision
    5928             : ***************************************/
    5929      811336 : uint32_t product_full_mode_decision(
    5930             :     struct ModeDecisionContext   *context_ptr,
    5931             :     CodingUnit                   *cu_ptr,
    5932             :     ModeDecisionCandidateBuffer **buffer_ptr_array,
    5933             :     uint32_t                      candidate_total_count,
    5934             :     uint32_t                     *best_candidate_index_array,
    5935             :     uint8_t                       prune_ref_frame_for_rec_partitions,
    5936             :     uint32_t                     *best_intra_mode)
    5937             : {
    5938             :     uint32_t                  candidateIndex;
    5939      811336 :     uint64_t                  lowestCost = 0xFFFFFFFFFFFFFFFFull;
    5940      811336 :     uint64_t                  lowestIntraCost = 0xFFFFFFFFFFFFFFFFull;
    5941      811336 :     uint32_t                  lowestCostIndex = 0;
    5942      811336 :     if (prune_ref_frame_for_rec_partitions) {
    5943       75584 :         if (context_ptr->blk_geom->shape == PART_N) {
    5944      224917 :             for (uint32_t i = 0; i < candidate_total_count; ++i) {
    5945      149303 :                 candidateIndex = best_candidate_index_array[i];
    5946      149303 :                 ModeDecisionCandidate *candidate_ptr = buffer_ptr_array[candidateIndex]->candidate_ptr;
    5947      149303 :                 EbBool is_inter = (candidate_ptr->pred_mode >= NEARESTMV) ? EB_TRUE : EB_FALSE;
    5948      149303 :                 EbBool is_simple_translation = (candidate_ptr->motion_mode != WARPED_CAUSAL) ? EB_TRUE : EB_FALSE;
    5949      149303 :                 if (is_inter && is_simple_translation) {
    5950      104387 :                     uint8_t ref_frame_type = candidate_ptr->ref_frame_type;
    5951      104387 :                     assert(ref_frame_type < MAX_REF_TYPE_CAND);
    5952      104387 :                     context_ptr->ref_best_cost_sq_table[ref_frame_type] = *(buffer_ptr_array[candidateIndex]->full_cost_ptr);
    5953             :                 }
    5954             : 
    5955             :             }
    5956             :             //Sort ref_frame by sq cost.
    5957             :             uint32_t i, j, index;
    5958     2339440 :             for (i = 0; i < MAX_REF_TYPE_CAND; ++i) {
    5959     2263830 :                 context_ptr->ref_best_ref_sq_table[i] = i;
    5960             :             }
    5961     2233480 :             for (i = 0; i < MAX_REF_TYPE_CAND - 1; ++i) {
    5962    34163900 :                 for (j = i + 1; j < MAX_REF_TYPE_CAND; ++j) {
    5963    32006000 :                     if (context_ptr->ref_best_cost_sq_table[context_ptr->ref_best_ref_sq_table[j]] < context_ptr->ref_best_cost_sq_table[context_ptr->ref_best_ref_sq_table[i]]) {
    5964       97500 :                         index = context_ptr->ref_best_ref_sq_table[i];
    5965       97500 :                         context_ptr->ref_best_ref_sq_table[i] = context_ptr->ref_best_ref_sq_table[j];
    5966       97500 :                         context_ptr->ref_best_ref_sq_table[j] = index;
    5967             :                     }
    5968             :                 }
    5969             :             }
    5970             :         }
    5971             :     }
    5972             : 
    5973             : 
    5974             :     PredictionUnit       *pu_ptr;
    5975             :     uint32_t                   i;
    5976             :     ModeDecisionCandidate       *candidate_ptr;
    5977             : 
    5978      811336 :     lowestCostIndex = best_candidate_index_array[0];
    5979             : 
    5980             :     // Find the candidate with the lowest cost
    5981     4736810 :     for (i = 0; i < candidate_total_count; ++i) {
    5982     3925470 :         candidateIndex = best_candidate_index_array[i];
    5983             : 
    5984             :         // Compute fullCostBis
    5985     3925470 :         if ((*(buffer_ptr_array[candidateIndex]->full_cost_ptr) < lowestIntraCost) && buffer_ptr_array[candidateIndex]->candidate_ptr->type == INTRA_MODE) {
    5986      285421 :             *best_intra_mode = buffer_ptr_array[candidateIndex]->candidate_ptr->pred_mode;
    5987      285421 :             lowestIntraCost = *(buffer_ptr_array[candidateIndex]->full_cost_ptr);
    5988             :         }
    5989             : 
    5990     3925470 :         if (*(buffer_ptr_array[candidateIndex]->full_cost_ptr) < lowestCost) {
    5991     1357930 :             lowestCostIndex = candidateIndex;
    5992     1357930 :             lowestCost = *(buffer_ptr_array[candidateIndex]->full_cost_ptr);
    5993             :         }
    5994             :     }
    5995             : 
    5996      811336 :     candidate_ptr = buffer_ptr_array[lowestCostIndex]->candidate_ptr;
    5997             : 
    5998      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].cost = *(buffer_ptr_array[lowestCostIndex]->full_cost_ptr);
    5999      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].cost = (context_ptr->md_local_cu_unit[cu_ptr->mds_idx].cost - buffer_ptr_array[lowestCostIndex]->candidate_ptr->chroma_distortion) + buffer_ptr_array[lowestCostIndex]->candidate_ptr->chroma_distortion_inter_depth;
    6000             : 
    6001      811336 :     if (candidate_ptr->type == INTRA_MODE)
    6002       77536 :         context_ptr->md_local_cu_unit[cu_ptr->mds_idx].cost_luma = buffer_ptr_array[lowestCostIndex]->full_cost_luma;
    6003             : 
    6004      811336 :     context_ptr->md_ep_pipe_sb[cu_ptr->mds_idx].merge_cost = *buffer_ptr_array[lowestCostIndex]->full_cost_merge_ptr;
    6005      811336 :     context_ptr->md_ep_pipe_sb[cu_ptr->mds_idx].skip_cost = *buffer_ptr_array[lowestCostIndex]->full_cost_skip_ptr;
    6006             : 
    6007      811336 :     if (candidate_ptr->type == INTER_MODE && candidate_ptr->merge_flag == EB_TRUE)
    6008      153799 :         context_ptr->md_ep_pipe_sb[cu_ptr->mds_idx].chroma_distortion = buffer_ptr_array[lowestCostIndex]->candidate_ptr->chroma_distortion;
    6009      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].full_distortion = buffer_ptr_array[lowestCostIndex]->candidate_ptr->full_distortion;
    6010      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].chroma_distortion = (uint32_t)buffer_ptr_array[lowestCostIndex]->candidate_ptr->chroma_distortion;
    6011      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].chroma_distortion_inter_depth = (uint32_t)buffer_ptr_array[lowestCostIndex]->candidate_ptr->chroma_distortion_inter_depth;
    6012             : 
    6013      811336 :     cu_ptr->prediction_mode_flag = candidate_ptr->type;
    6014      811336 :     cu_ptr->tx_depth = candidate_ptr->tx_depth;
    6015      811336 :     cu_ptr->skip_flag = candidate_ptr->skip_flag; // note, the skip flag is re-checked in the ENCDEC process
    6016      811336 :     cu_ptr->block_has_coeff = ((candidate_ptr->block_has_coeff) > 0) ? EB_TRUE : EB_FALSE;
    6017      811336 :     cu_ptr->quantized_dc[1][0] = buffer_ptr_array[lowestCostIndex]->candidate_ptr->quantized_dc[1][0];
    6018      811336 :     cu_ptr->quantized_dc[2][0] = buffer_ptr_array[lowestCostIndex]->candidate_ptr->quantized_dc[2][0];
    6019      811336 :     context_ptr->md_local_cu_unit[cu_ptr->mds_idx].count_non_zero_coeffs = candidate_ptr->count_non_zero_coeffs;
    6020             : 
    6021      811336 :     cu_ptr->av1xd->use_intrabc = candidate_ptr->use_intrabc;
    6022      811336 :     if (cu_ptr->prediction_mode_flag == INTER_MODE && candidate_ptr->is_compound)
    6023             :     {
    6024      363375 :         cu_ptr->interinter_comp.type = candidate_ptr->interinter_comp.type;
    6025      363375 :         cu_ptr->interinter_comp.mask_type = candidate_ptr->interinter_comp.mask_type;
    6026      363375 :         cu_ptr->interinter_comp.wedge_index = candidate_ptr->interinter_comp.wedge_index;
    6027      363375 :         cu_ptr->interinter_comp.wedge_sign = candidate_ptr->interinter_comp.wedge_sign;
    6028      363375 :         cu_ptr->compound_idx = candidate_ptr->compound_idx;
    6029      363375 :         cu_ptr->comp_group_idx = candidate_ptr->comp_group_idx;
    6030      363375 :         if (cu_ptr->interinter_comp.type == COMPOUND_AVERAGE){
    6031      295005 :             if (cu_ptr->comp_group_idx != 0 || cu_ptr->compound_idx != 1)
    6032           0 :                 printf("Error: Compound combination not allowed\n");
    6033             :         }
    6034             :     }
    6035             : #if II_COMP_FLAG
    6036             : 
    6037      811423 :     cu_ptr->is_interintra_used          = candidate_ptr->is_interintra_used;
    6038      811423 :     cu_ptr->interintra_mode             = candidate_ptr->interintra_mode;
    6039      811423 :     cu_ptr->use_wedge_interintra        = candidate_ptr->use_wedge_interintra;
    6040      811423 :     cu_ptr->interintra_wedge_index      = candidate_ptr->interintra_wedge_index;
    6041      811423 :     cu_ptr->ii_wedge_sign               = candidate_ptr->ii_wedge_sign;
    6042             : 
    6043             : #endif
    6044             :     // Set the PU level variables
    6045      811423 :     cu_ptr->interp_filters = candidate_ptr->interp_filters;
    6046             :     {
    6047      811423 :         pu_ptr = cu_ptr->prediction_unit_array;
    6048             :         // Intra Prediction
    6049      811423 :         pu_ptr->intra_luma_mode = 0x1F;
    6050      811423 :         if (cu_ptr->prediction_mode_flag == INTRA_MODE)
    6051             :         {
    6052             : #if FILTER_INTRA_FLAG
    6053       77536 :             cu_ptr->filter_intra_mode= candidate_ptr->filter_intra_mode;
    6054             : #endif
    6055       77536 :             pu_ptr->intra_luma_mode = candidate_ptr->intra_luma_mode;
    6056             : 
    6057       77536 :             pu_ptr->is_directional_mode_flag = candidate_ptr->is_directional_mode_flag;
    6058       77536 :             pu_ptr->angle_delta[PLANE_TYPE_Y] = candidate_ptr->angle_delta[PLANE_TYPE_Y];
    6059             : 
    6060       77536 :             pu_ptr->cfl_alpha_idx = candidate_ptr->cfl_alpha_idx;
    6061       77536 :             pu_ptr->cfl_alpha_signs = candidate_ptr->cfl_alpha_signs;
    6062             : 
    6063       77536 :             pu_ptr->intra_chroma_mode = candidate_ptr->intra_chroma_mode;
    6064       77536 :             pu_ptr->is_directional_chroma_mode_flag = candidate_ptr->is_directional_chroma_mode_flag;
    6065       77536 :             pu_ptr->angle_delta[PLANE_TYPE_UV] = candidate_ptr->angle_delta[PLANE_TYPE_UV];
    6066             :         }
    6067             : 
    6068             : #if PAL_SUP
    6069      811423 :         if (cu_ptr->prediction_mode_flag == INTRA_MODE)
    6070             :         {
    6071       77537 :             memcpy(&cu_ptr->palette_info.pmi, &candidate_ptr->palette_info.pmi, sizeof(PaletteModeInfo));
    6072       77537 :             if(svt_av1_allow_palette(context_ptr->sb_ptr->picture_control_set_ptr->parent_pcs_ptr->palette_mode, context_ptr->blk_geom->bsize))
    6073           0 :                memcpy(cu_ptr->palette_info.color_idx_map, candidate_ptr->palette_info.color_idx_map, MAX_PALETTE_SQUARE);
    6074             :         }
    6075             :         else {
    6076      733886 :             cu_ptr->palette_info.pmi.palette_size[0] = cu_ptr->palette_info.pmi.palette_size[1] = 0;
    6077             :         }
    6078             : #endif
    6079             :         // Inter Prediction
    6080      811423 :         pu_ptr->inter_pred_direction_index = candidate_ptr->prediction_direction[0];
    6081      811423 :         pu_ptr->merge_flag = candidate_ptr->merge_flag;
    6082      811423 :         if (cu_ptr->prediction_mode_flag != INTER_MODE && cu_ptr->av1xd->use_intrabc == 0)
    6083             :         {
    6084       77537 :             pu_ptr->inter_pred_direction_index = 0x03;
    6085       77537 :             pu_ptr->merge_flag = EB_FALSE;
    6086             :         }
    6087      811423 :         pu_ptr->mv[REF_LIST_0].x = 0;
    6088      811423 :         pu_ptr->mv[REF_LIST_0].y = 0;
    6089             : 
    6090      811423 :         pu_ptr->mv[REF_LIST_1].x = 0;
    6091      811423 :         pu_ptr->mv[REF_LIST_1].y = 0;
    6092             : 
    6093      811423 :         cu_ptr->pred_mode = candidate_ptr->pred_mode;
    6094      811423 :         cu_ptr->drl_index = candidate_ptr->drl_index;
    6095             : 
    6096      811423 :         pu_ptr->inter_mode = candidate_ptr->inter_mode;
    6097      811423 :         pu_ptr->is_compound = candidate_ptr->is_compound;
    6098      811423 :         pu_ptr->compound_idx = candidate_ptr->compound_idx;
    6099      811423 :         pu_ptr->interinter_comp = candidate_ptr->interinter_comp;
    6100      811423 :         pu_ptr->pred_mv_weight = candidate_ptr->pred_mv_weight;
    6101      811423 :         pu_ptr->ref_frame_type = candidate_ptr->ref_frame_type;
    6102      811423 :         pu_ptr->ref_frame_index_l0 = candidate_ptr->ref_frame_index_l0;
    6103      811423 :         pu_ptr->ref_frame_index_l1 = candidate_ptr->ref_frame_index_l1;
    6104      811423 :         pu_ptr->ref_mv_index = candidate_ptr->ref_mv_index;
    6105      811423 :         pu_ptr->is_new_mv = candidate_ptr->is_new_mv;
    6106      811423 :         pu_ptr->is_zero_mv = candidate_ptr->is_zero_mv;
    6107             : 
    6108      811423 :         if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_0)
    6109             :         {
    6110             :             //EB_MEMCPY(&pu_ptr->mv[REF_LIST_0].x,&candidate_ptr->mvs_l0,4);
    6111      239338 :             pu_ptr->mv[REF_LIST_0].x = candidate_ptr->motion_vector_xl0;
    6112      239338 :             pu_ptr->mv[REF_LIST_0].y = candidate_ptr->motion_vector_yl0;
    6113             :         }
    6114             : 
    6115      811423 :         if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_1)
    6116             :         {
    6117             :             //EB_MEMCPY(&pu_ptr->mv[REF_LIST_1].x,&candidate_ptr->mvs_l1,4);
    6118      131193 :             pu_ptr->mv[REF_LIST_1].x = candidate_ptr->motion_vector_xl1;
    6119      131193 :             pu_ptr->mv[REF_LIST_1].y = candidate_ptr->motion_vector_yl1;
    6120             :         }
    6121             : 
    6122      811423 :         if (pu_ptr->inter_pred_direction_index == BI_PRED)
    6123             :         {
    6124             :             //EB_MEMCPY(&pu_ptr->mv[REF_LIST_0].x,&candidate_ptr->mvs,8);
    6125      363386 :             pu_ptr->mv[REF_LIST_0].x = candidate_ptr->motion_vector_xl0;
    6126      363386 :             pu_ptr->mv[REF_LIST_0].y = candidate_ptr->motion_vector_yl0;
    6127      363386 :             pu_ptr->mv[REF_LIST_1].x = candidate_ptr->motion_vector_xl1;
    6128      363386 :             pu_ptr->mv[REF_LIST_1].y = candidate_ptr->motion_vector_yl1;
    6129             :         }
    6130             : #if FIX_NEAREST_NEW
    6131      811423 :         if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_0) {
    6132      239339 :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_0];
    6133      239339 :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_0];
    6134             :         }
    6135      572084 :         else if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_1) {
    6136      131193 :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_1];
    6137      131193 :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_1];
    6138             :         }
    6139      440891 :         else if (pu_ptr->inter_pred_direction_index == BI_PRED) {
    6140      363387 :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_0];
    6141      363387 :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_0];
    6142      363387 :             cu_ptr->predmv[1].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_1];
    6143      363387 :             cu_ptr->predmv[1].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_1];
    6144             :         }
    6145             : #endif
    6146             :         // The MV prediction indicies are recalcated by the EncDec.
    6147      811423 :         pu_ptr->mvd[REF_LIST_0].pred_idx = 0;
    6148      811423 :         pu_ptr->mvd[REF_LIST_1].pred_idx = 0;
    6149             : 
    6150      811423 :         pu_ptr->overlappable_neighbors[0] = context_ptr->cu_ptr->prediction_unit_array[0].overlappable_neighbors[0];
    6151      811423 :         pu_ptr->overlappable_neighbors[1] = context_ptr->cu_ptr->prediction_unit_array[0].overlappable_neighbors[1];
    6152      811423 :         pu_ptr->motion_mode = candidate_ptr->motion_mode;
    6153      811423 :         pu_ptr->num_proj_ref = candidate_ptr->num_proj_ref;
    6154      811423 :         if (pu_ptr->motion_mode == WARPED_CAUSAL) {
    6155      201351 :             EB_MEMCPY(&pu_ptr->wm_params_l0, &candidate_ptr->wm_params_l0, sizeof(EbWarpedMotionParams));
    6156      201352 :             EB_MEMCPY(&pu_ptr->wm_params_l1, &candidate_ptr->wm_params_l1, sizeof(EbWarpedMotionParams));
    6157             :         }
    6158             :     }
    6159             : 
    6160             :     TransformUnit *txb_ptr;
    6161             :     uint32_t txb_itr;
    6162             :     uint32_t tu_index;
    6163             :     uint32_t tuTotalCount;
    6164      811432 :     uint32_t cu_size_log2 = context_ptr->cu_size_log2;
    6165      811432 :     tuTotalCount = context_ptr->blk_geom->txb_count[cu_ptr->tx_depth];
    6166      811432 :     tu_index = 0;
    6167      811432 :     txb_itr = 0;
    6168             : #if NO_ENCDEC
    6169             :     int32_t txb_1d_offset = 0, txb_1d_offset_uv = 0;
    6170             : 
    6171             :     cu_ptr->block_has_coeff = 0;
    6172             : #endif
    6173             : 
    6174             :     //cu_ptr->forceSmallTu = candidate_ptr->forceSmallTu;
    6175             : 
    6176             :     // Set TU
    6177             :     do {
    6178      877597 :         txb_ptr = &cu_ptr->transform_unit_array[tu_index];
    6179             : 
    6180      877597 :         txb_ptr->split_flag = EB_FALSE;
    6181      877597 :         txb_ptr->y_has_coeff = (EbBool)(((candidate_ptr->y_has_coeff)  & (1 << tu_index)) > 0);
    6182      877597 :         txb_ptr->u_has_coeff = (EbBool)(((candidate_ptr->u_has_coeff) & (1 << (tu_index))) > 0);
    6183      877597 :         txb_ptr->v_has_coeff = (EbBool)(((candidate_ptr->v_has_coeff) & (1 << (tu_index))) > 0);
    6184      877597 :         txb_ptr->transform_type[PLANE_TYPE_Y] = candidate_ptr->transform_type[tu_index];
    6185      877597 :         txb_ptr->transform_type[PLANE_TYPE_UV] = candidate_ptr->transform_type_uv;
    6186             : 
    6187      877597 :         cu_ptr->quantized_dc[0][tu_index] = candidate_ptr->quantized_dc[0][tu_index];
    6188             : 
    6189             : #if NO_ENCDEC
    6190             : 
    6191             :         if (context_ptr->blk_geom->has_uv) {
    6192             :             cu_ptr->block_has_coeff |= txb_ptr->y_has_coeff;
    6193             :             cu_ptr->block_has_coeff |= txb_ptr->u_has_coeff;
    6194             :             cu_ptr->block_has_coeff |= txb_ptr->v_has_coeff;
    6195             :         }
    6196             :         else
    6197             :             cu_ptr->block_has_coeff |= txb_ptr->y_has_coeff;
    6198             :         cu_ptr->cand_buff_index = lowestCostIndex;
    6199             : 
    6200             :         cu_ptr->skip_flag = 0;   //SKIP is turned OFF for this case!!
    6201             :         txb_ptr->nz_coef_count[0] = candidate_ptr->eob[0][tu_index];
    6202             :         txb_ptr->nz_coef_count[1] = candidate_ptr->eob[1][tu_index];
    6203             :         txb_ptr->nz_coef_count[2] = candidate_ptr->eob[2][tu_index];
    6204             : 
    6205             :         if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_0) {
    6206             :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_0];
    6207             :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_0];
    6208             :         }
    6209             :         else if (pu_ptr->inter_pred_direction_index == UNI_PRED_LIST_1) {
    6210             :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_1];
    6211             :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_1];
    6212             :         }
    6213             :         else if (pu_ptr->inter_pred_direction_index == BI_PRED) {
    6214             :             cu_ptr->predmv[0].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_0];
    6215             :             cu_ptr->predmv[0].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_0];
    6216             :             cu_ptr->predmv[1].as_mv.col = candidate_ptr->motion_vector_pred_x[REF_LIST_1];
    6217             :             cu_ptr->predmv[1].as_mv.row = candidate_ptr->motion_vector_pred_y[REF_LIST_1];
    6218             :         }
    6219             : #endif
    6220             : #if NO_ENCDEC
    6221             :         //copy coeff
    6222             :         {
    6223             :             uint32_t  bwidth = context_ptr->blk_geom->tx_width[txb_itr] < 64 ? context_ptr->blk_geom->tx_width[txb_itr] : 32;
    6224             :             uint32_t  bheight = context_ptr->blk_geom->tx_height[txb_itr] < 64 ? context_ptr->blk_geom->tx_height[txb_itr] : 32;
    6225             : 
    6226             :             int32_t* src_ptr = &(((int32_t*)buffer_ptr_array[lowestCostIndex]->residual_quant_coeff_ptr->buffer_y)[txb_1d_offset]);
    6227             :             int32_t* dst_ptr = &(((int32_t*)context_ptr->cu_ptr->coeff_tmp->buffer_y)[txb_1d_offset]);
    6228             : 
    6229             :             uint32_t j;
    6230             : 
    6231             :             for (j = 0; j < bheight; j++)
    6232             :                 memcpy(dst_ptr + j * bwidth, src_ptr + j * bwidth, bwidth * sizeof(int32_t));
    6233             :             if (context_ptr->blk_geom->has_uv)
    6234             :             {
    6235             :                 // Cb
    6236             :                 bwidth = context_ptr->blk_geom->tx_width_uv[txb_itr];
    6237             :                 bheight = context_ptr->blk_geom->tx_height_uv[txb_itr];
    6238             : 
    6239             :                 src_ptr = &(((int32_t*)buffer_ptr_array[lowestCostIndex]->residual_quant_coeff_ptr->buffer_cb)[txb_1d_offset_uv]);
    6240             :                 dst_ptr = &(((int32_t*)context_ptr->cu_ptr->coeff_tmp->buffer_cb)[txb_1d_offset_uv]);
    6241             : 
    6242             :                 for (j = 0; j < bheight; j++)
    6243             :                     memcpy(dst_ptr + j * bwidth, src_ptr + j * bwidth, bwidth * sizeof(int32_t));
    6244             :                 src_ptr = &(((int32_t*)buffer_ptr_array[lowestCostIndex]->residual_quant_coeff_ptr->buffer_cr)[txb_1d_offset_uv]);
    6245             :                 dst_ptr = &(((int32_t*)context_ptr->cu_ptr->coeff_tmp->buffer_cr)[txb_1d_offset_uv]);
    6246             : 
    6247             :                 for (j = 0; j < bheight; j++)
    6248             :                     memcpy(dst_ptr + j * bwidth, src_ptr + j * bwidth, bwidth * sizeof(int32_t));
    6249             :             }
    6250             : 
    6251             :             txb_1d_offset += context_ptr->blk_geom->tx_width[txb_itr] * context_ptr->blk_geom->tx_height[txb_itr];
    6252             :             if (context_ptr->blk_geom->has_uv)
    6253             :                 txb_1d_offset_uv += context_ptr->blk_geom->tx_width_uv[txb_itr] * context_ptr->blk_geom->tx_height_uv[txb_itr];
    6254             :         }
    6255             : 
    6256             : #endif
    6257             : 
    6258      877597 :         ++tu_index;
    6259      877597 :         ++txb_itr;
    6260      877597 :     } while (txb_itr < tuTotalCount);
    6261             :     UNUSED(cu_size_log2);
    6262      811432 :     return lowestCostIndex;
    6263             : }

Generated by: LCOV version 1.14