/*!
 ***************************************************************************
 * \file md_high.c
 *
 * \brief
 *    Main macroblock mode decision functions and helpers
 *
 **************************************************************************
 */

#include <math.h>
#include <limits.h>
#include <float.h>

#include "global.h"
#include "rdopt_coding_state.h"
#include "intrarefresh.h"
#include "image.h"
#include "ratectl.h"
#include "mode_decision.h"
#include "mode_decision_p8x8.h"
#include "fmo.h"
#include "me_umhex.h"
#include "me_umhexsmp.h"
#include "macroblock.h"
#include "md_common.h"
#include "conformance.h"
#include "vlc.h"
#include "rdopt.h"
#include "mv_search.h"

#if QC_GVSP_B0054
void set_curr_vsp_ref(Macroblock *currMB, int mode)
{
  int i;
  if (mode<PVSP_SKIP)
  {
    currMB->isVspRef = 0;
    for (i=0; i<4; i++)
      currMB->isPartVspRef[i] =0;
    return;
  }
  else
  {
    switch (mode)
    {
    case PVSP_SKIP:
      currMB->isVspRef = 1;
      for (i=0; i<4; i++)
        currMB->isPartVspRef[i] = 1;			
      break;

    case P16x16_VSP:
      currMB->isVspRef = 1;
      for (i=0; i<4; i++)
        currMB->isPartVspRef[i] = 1;			
      break;

    case P16x8_UP_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = 1;
      currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 0;
      break;

    case P16x8_DN_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = 0;
      currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 1;			
      break;

    case P8x16_LT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = 1;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 0;			
      break;

    case P8x16_RT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = 0;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 1;
      break;

    case P8x8_UPLT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = 1;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 0;
      break;

    case P8x8_UPLT_NVSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = 0;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 1;
      break;

    case P8x8_UPRT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[1] = 1;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 0;
      break;

    case P8x8_UPRT_NVSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[1] = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 1;
      break;

    case P8x8_DNLT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[2] = 1;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 0;
      break;

    case P8x8_DNLT_NVSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[2] = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 1;
      break;

    case P8x8_DNRT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[3] = 1;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = 0;
      break;

    case P8x8_DNRT_NVSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[3] = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = 1;
      break;

    case P8x8_LT_DIAG_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[3] = 1;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = 0;
      break;

    case P8x8_RT_DIAG_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[3] = 0;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[2] = 1;
      break;

    case P8x8_UP_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = 1;
      currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 0;
      break;

    case P8x8_DN_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[1] = 0;
      currMB->isPartVspRef[2] = currMB->isPartVspRef[3] = 1;
      break;

    case P8x8_LT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = 1;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 0;
      break;

    case P8x8_RT_VSP:
      currMB->isVspRef = 0;
      currMB->isPartVspRef[0] = currMB->isPartVspRef[2] = 0;
      currMB->isPartVspRef[1] = currMB->isPartVspRef[3] = 1;
      break;

    default:
      printf ("Unsupported mode in set_curr_vsp_ref!\n");
      exit (1);
    }
  }

  return;
}


int virtualMode2Mode(Macroblock *currMB, int mode) 
{
  if (mode<PVSP_SKIP)
  {
    return mode;
  }
  else
  {
    switch (mode)
    {
    case PVSP_SKIP:
      return PSKIP;
      break;

    case P16x16_VSP:
      return P16x16;
      break;

    case P16x8_UP_VSP:
    case P16x8_DN_VSP:
      return P16x8;
      break;

    case P8x16_LT_VSP:
    case P8x16_RT_VSP:
      return P8x16;
      break;

    case P8x8_UPLT_VSP:
    case P8x8_UPLT_NVSP:
    case P8x8_UPRT_VSP:
    case P8x8_UPRT_NVSP:
    case P8x8_DNLT_VSP:
    case P8x8_DNLT_NVSP:
    case P8x8_DNRT_VSP:
    case P8x8_DNRT_NVSP:
    case P8x8_LT_DIAG_VSP:
    case P8x8_RT_DIAG_VSP:
    case P8x8_UP_VSP:
    case P8x8_DN_VSP:
    case P8x8_LT_VSP:
    case P8x8_RT_VSP:
      return P8x8;
      break;
		default:
			assert(0);
			return 0;
    }
  }
}
#endif

/*!
*************************************************************************************
* \brief
*    Mode Decision for a macroblock
*************************************************************************************
*/
void encode_one_macroblock_high (Macroblock *currMB)
{
  Slice *currSlice = currMB->p_Slice;
  VideoParameters *p_Vid = currMB->p_Vid;
  InputParameters *p_Inp = currMB->p_Inp;
  PicMotionParams **motion = p_Vid->enc_picture->mv_info;
  RDOPTStructure  *p_RDO = currSlice->p_RDO;

#if SMRC_ALC_M26683 
  short cur_best_mode = currMB->best_mode;
#endif

#if IVMP
#if MERL_VSP
  int         max_index = 11; // Dong
#else
  #if QC_GVSP_B0054
  int         max_index = 26;
  #else
  int         max_index = (p_Inp->InsideViewMVP && currSlice->ivmp_enable_flag )?10:9;
  #endif
#endif
#else
#if MERL_VSP
  int         max_index = 11;
#else
  #if QC_GVSP_B0054
  int         max_index = 26;
  #else
  int         max_index = 9;
  #endif
#endif
#endif

  int         block, index, mode, i, j;
  RD_PARAMS   enc_mb;
  distblk     bmcost[5] = {DISTBLK_MAX};
  distblk     cost=0;
  distblk     min_cost = DISTBLK_MAX;
#if SMRC_ALC_M26683 == 0
  int         intra1 = 0;
#endif

  int         mb_available[3];

  short       bslice      = (short) (currSlice->slice_type == B_SLICE);
  short       pslice      = (short) ((currSlice->slice_type == P_SLICE) || (currSlice->slice_type == SP_SLICE));
  short       intra       = (short) ((currSlice->slice_type == I_SLICE) || (currSlice->slice_type == SI_SLICE) || (pslice && currMB->mb_y == p_Vid->mb_y_upd && p_Vid->mb_y_upd != p_Vid->mb_y_intra));
  int         lambda_mf[3];

  imgpel    **mb_pred  = currSlice->mb_pred[0];
  Block8x8Info *b8x8info = p_Vid->b8x8info;

  char        chroma_pred_mode_range[2];
  short       inter_skip = 0;
  BestMode    md_best;
  Info8x8     best;


  init_md_best(&md_best);

  // Init best (need to create simple function)
  best.pdir = 0;
  best.bipred = 0;
  best.ref[LIST_0] = 0;
  best.ref[LIST_1] = -1;

  intra |= RandomIntra (p_Vid, currMB->mbAddrX);    // Forced Pseudo-Random Intra

  //===== Setup Macroblock encoding parameters =====
  init_enc_mb_params(currMB, &enc_mb, intra);

#if SMRC_ALC_M26683 
  // set possible coding modes for ALC
  if (currMB->is_alc_mode)
  {
      init_alc_enc_mb_params(currMB, &enc_mb);
  }
#endif

  if (p_Inp->AdaptiveRounding)
  {
    reset_adaptive_rounding(p_Vid);
  }

  if (currSlice->mb_aff_frame_flag)
  {
    reset_mb_nz_coeff(p_Vid, currMB->mbAddrX);
  }

  //=====   S T O R E   C O D I N G   S T A T E   =====
  //---------------------------------------------------
  currSlice->store_coding_state (currMB, currSlice->p_RDO->cs_cm);

  if (!intra)
  {
    //===== set skip/direct motion vectors =====
    if (enc_mb.valid[0])
    {
      if (bslice)
        currSlice->Get_Direct_Motion_Vectors (currMB);
      else 
#if MTK_SKIP_DIRECT_A0045
      {
        if(currSlice->depth_based_mvp_flag)
#if SMRC_ALC_M26683
		{
			if(currMB->is_alc_mode)
				FindSkipModeMotionVector_4ALC(currMB);
			else
#endif
				FindSkipModeMotionVector_MVP (currMB);
#if SMRC_ALC_M26683
		}
#endif
        else
          FindSkipModeMotionVector(currMB);
      }
#elif DEPTH_BASED_MVP
	  {
      if(currSlice->depth_based_mvp_flag)
			  FindMVCSkipModeMotionVector(currMB);
		  else
			  FindSkipModeMotionVector(currMB);
	  }
#else
        FindSkipModeMotionVector (currMB);
#endif
    }
#if MERL_VSP
    if (enc_mb.valid[PVSP_SKIP])
    {
      if (bslice)
        FindVSPDirectModeMotionVector(currMB, b8x8info);
      else
        FindVSPSkipModeMotionVector(currMB, b8x8info);
    }
#endif
    if (p_Inp->CtxAdptLagrangeMult == 1)
    {
      get_initial_mb16x16_cost(currMB);
    }

    //===== MOTION ESTIMATION FOR 16x16, 16x8, 8x16 BLOCKS =====
    for (mode = 1; mode < 4; mode++)
    {
      best.mode = (char) mode;
      best.bipred = 0;
      b8x8info->best[mode][0].bipred = 0;

      if (enc_mb.valid[mode])
      {
        for (cost=0, block=0; block<(mode==1?1:2); block++)
        {
          update_lambda_costs(currMB, &enc_mb, lambda_mf);
          PartitionMotionSearch (currMB, mode, block, lambda_mf);

          //--- set 4x4 block indices (for getting MV) ---
          j = (block==1 && mode==2 ? 2 : 0);
          i = (block==1 && mode==3 ? 2 : 0);

          //--- get cost and reference frame for List 0 prediction ---
          bmcost[LIST_0] = DISTBLK_MAX;
          list_prediction_cost(currMB, LIST_0, block, mode, &enc_mb, bmcost, best.ref);

          if (bslice)
          {
            //--- get cost and reference frame for List 1 prediction ---
            bmcost[LIST_1] = DISTBLK_MAX;
            list_prediction_cost(currMB, LIST_1, block, mode, &enc_mb, bmcost, best.ref);

            // Compute bipredictive cost between best list 0 and best list 1 references
            list_prediction_cost(currMB, BI_PRED, block, mode, &enc_mb, bmcost, best.ref);

            // currently Bi predictive ME is only supported for modes 1, 2, 3 and ref 0
            if (is_bipred_enabled(p_Vid, mode))
            {
              get_bipred_cost(currMB, mode, block, i, j, &best, &enc_mb, bmcost);
            }
            else
            {
              bmcost[BI_PRED_L0] = DISTBLK_MAX;
              bmcost[BI_PRED_L1] = DISTBLK_MAX;
            }

            // Determine prediction list based on mode cost
            determine_prediction_list(bmcost, &best, &cost);
          }
          else // if (bslice)
          {
            best.pdir = 0;
            cost      += bmcost[LIST_0];
          }

          assign_enc_picture_params(currMB, mode, &best, 2 * block);

          //----- set reference frame and direction parameters -----
          set_block8x8_info(b8x8info, mode, block, &best);

          //--- set reference frames and motion vectors ---
          if (mode>1 && block == 0)
            currSlice->set_ref_and_motion_vectors (currMB, motion, &best, block);
        } // for (block=0; block<(mode==1?1:2); block++)
        if (cost < min_cost)
        {
          md_best.mode = (byte) mode;
          md_best.cost = cost;
#if SMRC_ALC_M26683 
          cur_best_mode = (short) mode;
#else
          currMB->best_mode = (short) mode;
#endif
          min_cost  = cost;
          if (p_Inp->CtxAdptLagrangeMult == 1)
          {
            adjust_mb16x16_cost(currMB, cost);
          }
        }
      } // if (enc_mb.valid[mode])
    } // for (mode=1; mode<4; mode++)

    if (enc_mb.valid[P8x8])
    {    
      currMB->valid_8x8 = FALSE;

      if (p_Inp->Transform8x8Mode)
      {
        ResetRD8x8Data(p_Vid, p_RDO->tr8x8);
        currMB->luma_transform_size_8x8_flag = TRUE; //switch to 8x8 transform size
        //===========================================================
        // Check 8x8 partition with transform size 8x8
        //===========================================================
        //=====  LOOP OVER 8x8 SUB-PARTITIONS  (Motion Estimation & Mode Decision) =====
        for (block = 0; block < 4; block++)
        {
          currSlice->submacroblock_mode_decision(currMB, &enc_mb, p_RDO->tr8x8, p_RDO->cofAC8x8ts[block], block, &cost);
          if(!currMB->valid_8x8)
            break;
          set_subblock8x8_info(b8x8info, P8x8, block, p_RDO->tr8x8);
        } 

      }// if (p_Inp->Transform8x8Mode)

      currMB->valid_4x4 = FALSE;
      if (p_Inp->Transform8x8Mode != 2)
      {
        currMB->luma_transform_size_8x8_flag = FALSE; //switch to 8x8 transform size
        ResetRD8x8Data(p_Vid, p_RDO->tr4x4);
        //=================================================================
        // Check 8x8, 8x4, 4x8 and 4x4 partitions with transform size 4x4
        //=================================================================
        //=====  LOOP OVER 8x8 SUB-PARTITIONS  (Motion Estimation & Mode Decision) =====
        for (block = 0; block < 4; block++)
        {
          currSlice->submacroblock_mode_decision(currMB, &enc_mb, p_RDO->tr4x4, p_RDO->coefAC8x8[block], block, &cost);
          if(!currMB->valid_4x4)
            break;
          set_subblock8x8_info(b8x8info, P8x8, block, p_RDO->tr4x4);
        }
      }// if (p_Inp->Transform8x8Mode != 2)

      if (p_Inp->RCEnable)
        rc_store_diff(currSlice->diffy, &p_Vid->pCurImg[currMB->opix_y], currMB->pix_x, mb_pred);

      p_Vid->giRDOpt_B8OnlyFlag = FALSE;
    }
  }
  else // if (!intra)
  {
    min_cost = DISTBLK_MAX;
  }

  // Set Chroma mode
  set_chroma_pred_mode(currMB, enc_mb, mb_available, chroma_pred_mode_range);

  //========= C H O O S E   B E S T   M A C R O B L O C K   M O D E =========
  //-------------------------------------------------------------------------

  for (currMB->c_ipred_mode = chroma_pred_mode_range[0]; currMB->c_ipred_mode<=chroma_pred_mode_range[1]; currMB->c_ipred_mode++)
  {
    // bypass if c_ipred_mode is not allowed
    if ( (p_Vid->yuv_format != YUV400) &&
      (  ((!intra || !p_Inp->IntraDisableInterOnly) && p_Inp->ChromaIntraDisable == 1 && currMB->c_ipred_mode!=DC_PRED_8) 
      || (currMB->c_ipred_mode == VERT_PRED_8 && !mb_available[0]) 
      || (currMB->c_ipred_mode == HOR_PRED_8  && !mb_available[1]) 
      || (currMB->c_ipred_mode == PLANE_8     && (!mb_available[1] || !mb_available[0] || !mb_available[2]))))
      continue;        

    //===== GET BEST MACROBLOCK MODE =====
    for (index=0; index < max_index; index++)
    {
#if IVMP
		if(index == 9)
		{
			int ivmp_type = currSlice->checkIVMPAvaiable(currMB);
			if(ivmp_type==1)
			{
				currMB->ivmp_used = 1; 
				currSlice->Get_ivmp_params(currMB);
			}else
				currMB->ivmp_used = 0;
			if(currMB->ivmp_used <= 0)
				continue;
			else
				mode = currMB->mb_type;
		}
		else
		{
			currMB->ivmp_used = 0;
			mode = mb_mode_table[index];
		}
		if(index == 9 ||(index != 9 && enc_mb.valid[mode]))
#else
      mode = mb_mode_table[index];
      if (enc_mb.valid[mode])
#endif
      {
#if QC_GVSP_B0054
        if (currSlice->bVSPEnabled)
        {
          set_curr_vsp_ref(currMB, mode);
          mode = virtualMode2Mode(currMB, mode);
        }
#endif
        if (p_Vid->yuv_format != YUV400)
        {           
          currMB->i16mode = 0; 
        }

        // Skip intra modes in inter slices if best mode is inter <P8x8 with cbp equal to 0    
#if SMRC_ALC_M26683 
        if (currSlice->P444_joined)
        {
          if (p_Inp->SkipIntraInInterSlices && !intra && mode >= I16MB 
            && cur_best_mode <=3 && currMB->best_cbp == 0 && currSlice->cmp_cbp[1] == 0 && currSlice->cmp_cbp[2] == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp,5)))
            continue;
        }
        else
        {
          if (p_Inp->SkipIntraInInterSlices)
          {
            if (!intra && mode >= I4MB)
            {
              if (cur_best_mode <=3 && currMB->best_cbp == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp, 5)))
              {
                continue;
              }
              else if (cur_best_mode == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp,6)))
              {
                continue;
              }
            }
          }
        }
#else // SMRC_ALC_M26683 
        if (currSlice->P444_joined)
        {
          if (p_Inp->SkipIntraInInterSlices && !intra && mode >= I16MB 
            && currMB->best_mode <=3 && currMB->best_cbp == 0 && currSlice->cmp_cbp[1] == 0 && currSlice->cmp_cbp[2] == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp,5)))
            continue;
        }
        else
        {
          if (p_Inp->SkipIntraInInterSlices)
          {
            if (!intra && mode >= I4MB)
            {
              if (currMB->best_mode <=3 && currMB->best_cbp == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp, 5)))
              {
                continue;
              }
              else if (currMB->best_mode == 0 && (currMB->min_rdcost < weighted_cost(enc_mb.lambda_mdfp,6)))
              {
                continue;
              }
            }
          }
        }
#endif // SMRC_ALC_M26683 

        compute_mode_RD_cost(currMB, &enc_mb, (short) mode, &inter_skip);

#if SAIT_DISP
        if(p_Vid->is_depth && mode == I16MB && p_Vid->EnableDISP) 
        {
          compute_mode_RD_cost(currMB, &enc_mb, (short) DISPMODE, &inter_skip);
        }
#endif
      }
    }// for (index=0; index<max_index; index++)
  }// for (currMB->c_ipred_mode=DC_PRED_8; currMB->c_ipred_mode<=chroma_pred_mode_range[1]; currMB->c_ipred_mode++)                     

  restore_nz_coeff(currMB);

#if SMRC_ALC_M26683 == 0
  intra1 = IS_INTRA(currMB);
#endif

  //=====  S E T   F I N A L   M A C R O B L O C K   P A R A M E T E R S ======
  //---------------------------------------------------------------------------
#if SMRC_ALC_M26683 == 0
  update_qp_cbp_tmp(currMB, p_RDO->cbp);
  currSlice->set_stored_mb_parameters (currMB);

  // Rate control
  if(p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE)
    rc_store_mad(currMB);


  //===== Decide if this MB will restrict the reference frames =====
  if (p_Inp->RestrictRef)
    update_refresh_map(currMB, intra, intra1);
#endif
}


