        }
        // In accumulating a score for the 2nd reference frame take the
        // best of the motion predicted score and the intra coded error
        // (just as will be done for) accumulation of "coded_error" for
        // the last frame.
        if (fast_candidate_total_count > 2) {
            stats->sr_coded_error += AOMMIN(gf_motion_error, this_intra_error);
        } else {
            stats->sr_coded_error += motion_error;
        }

        // Motion search in 3rd reference frame.
        int alt_motion_error = motion_error;
        if (alt_motion_error < motion_error && alt_motion_error < gf_motion_error &&
            alt_motion_error < this_intra_error) {
            ++stats->third_ref_count;
        }
        // In accumulating a score for the 3rd reference frame take the
        // best of the motion predicted score and the intra coded error
        // (just as will be done for) accumulation of "coded_error" for
        // the last frame.
        // alt_ref_frame is not supported yet
        if (0 /*alt_ref_frame!= NULL*/) {
            stats->tr_coded_error += AOMMIN(alt_motion_error, this_intra_error);
        } else {
            stats->tr_coded_error += motion_error;
        }
    } else {
        stats->sr_coded_error += motion_error;
        stats->tr_coded_error += motion_error;
    }

    // Start by assuming that intra mode is best.
    if (motion_error <= this_intra_error) {
#ifdef ARCH_X86
        aom_clear_system_state();
#endif
        // Keep a count of cases where the inter and intra were very close
        // and very low. This helps with scene cut detection for example in
        // cropped clips with black bars at the sides or top and bottom.
        if (((this_intra_error - INTRA_MODE_PENALTY) * 9 <= motion_error * 10) &&
            (this_intra_error < (2 * INTRA_MODE_PENALTY))) {
            stats->neutral_count += 1.0;
            // Also track cases where the intra is not much worse than the inter
            // and use this in limiting the GF/arf group length.
        } else if ((this_intra_error > NCOUNT_INTRA_THRESH) &&
                   (this_intra_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
            stats->neutral_count +=
                (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_intra_error);
        }
        const MV best_mv = get_mv_from_fullmv(&mv);
        this_inter_error = motion_error;
        stats->sum_mvr += best_mv.row;
        stats->sum_mvr_abs += abs(best_mv.row);
        stats->sum_mvc += best_mv.col;
        stats->sum_mvc_abs += abs(best_mv.col);
        stats->sum_mvrs += best_mv.row * best_mv.row;
        stats->sum_mvcs += best_mv.col * best_mv.col;
        ++stats->inter_count;
        accumulate_mv_stats(best_mv, mv, mb_row, mb_col, mb_rows, mb_cols, &last_mv, stats);
    }

    return this_inter_error;
}

void set_inter_comp_controls(ModeDecisionContext *mdctxt, uint8_t inter_comp_mode);
void set_dist_based_ref_pruning_controls(ModeDecisionContext *mdctxt, uint8_t dist_based_ref_pruning_level);

/******************************************************
* Derive md Settings(feature signals) that could be
  changed  at the block level
******************************************************/
extern EbErrorType first_pass_signal_derivation_block(
    ModeDecisionContext   *context_ptr) {

    EbErrorType return_error = EB_ErrorNone;


    // Set dist_based_ref_pruning
    context_ptr->dist_based_ref_pruning = 0;

    set_dist_based_ref_pruning_controls(context_ptr, context_ptr->dist_based_ref_pruning);

    // set compound_types_to_try
    set_inter_comp_controls(context_ptr, 0);

    context_ptr->compound_types_to_try = MD_COMP_AVG;

    // Do not add MD_COMP_WEDGE  beyond this point
    if (get_wedge_params_bits(context_ptr->blk_geom->bsize) == 0)
        context_ptr->compound_types_to_try = MIN(context_ptr->compound_types_to_try, MD_COMP_DIFF0);
    context_ptr->inject_inter_candidates = 1;

    return return_error;
}

void product_coding_loop_init_fast_loop(ModeDecisionContext *context_ptr,
                                        NeighborArrayUnit *  skip_coeff_neighbor_array,
                                        NeighborArrayUnit *  inter_pred_dir_neighbor_array,
                                        NeighborArrayUnit *  ref_frame_type_neighbor_array,
                                        NeighborArrayUnit *  intra_luma_mode_neighbor_array,
                                        NeighborArrayUnit *  skip_flag_neighbor_array,
                                        NeighborArrayUnit *  mode_type_neighbor_array,
                                        NeighborArrayUnit *  leaf_depth_neighbor_array,
                                        NeighborArrayUnit *  leaf_partition_neighbor_array);
void read_refine_me_mvs(PictureControlSet *pcs_ptr, ModeDecisionContext *context_ptr,
    EbPictureBufferDesc *input_picture_ptr, uint32_t input_origin_index,
    uint32_t blk_origin_index);
void perform_md_reference_pruning(PictureControlSet *pcs_ptr, ModeDecisionContext *context_ptr,
                         EbPictureBufferDesc *input_picture_ptr, uint32_t blk_origin_index);

// inject intra candidates for first pass
void  first_pass_inject_intra_candidates(
    ModeDecisionContext          *context_ptr,
    uint32_t                     *candidate_total_cnt){


    uint32_t                    cand_total_cnt = 0;
    ModeDecisionCandidate    *cand_array = context_ptr->fast_candidate_array;

    cand_array[cand_total_cnt].type = INTRA_MODE;
    cand_array[cand_total_cnt].merge_flag = EB_FALSE;

    cand_array[cand_total_cnt].palette_info = NULL;

    cand_array[cand_total_cnt].intra_luma_mode = DC_PRED;
    cand_array[cand_total_cnt].distortion_ready = 0;
    cand_array[cand_total_cnt].use_intrabc = 0;
    cand_array[cand_total_cnt].filter_intra_mode = FILTER_INTRA_MODES;
    cand_array[cand_total_cnt].is_directional_mode_flag = 0;
    cand_array[cand_total_cnt].angle_delta[PLANE_TYPE_Y] = 0;

    cand_array[cand_total_cnt].intra_chroma_mode = UV_DC_PRED;

    cand_array[cand_total_cnt].is_directional_chroma_mode_flag = 0;
    cand_array[cand_total_cnt].angle_delta[PLANE_TYPE_UV] = 0;

    cand_array[cand_total_cnt].cfl_alpha_signs = 0;
    cand_array[cand_total_cnt].cfl_alpha_idx = 0;
    cand_array[cand_total_cnt].transform_type[0] = DCT_DCT;
    cand_array[cand_total_cnt].transform_type_uv = DCT_DCT;

    cand_array[cand_total_cnt].ref_frame_type = INTRA_FRAME;
    cand_array[cand_total_cnt].pred_