コード例 #1
0
ファイル: img_distortion.c プロジェクト: Cheng-Hsu/JM
/*!
 ************************************************************************
 * \brief
 *    Find distortion for all three components
 ************************************************************************
 */
void find_distortion (ImageParameters *p_Img, InputParameters *p_Inp, ImageData *imgData)
{
  DistortionParams *p_Dist = p_Img->p_Dist;
  int64 diff_cmp[3] = {0};

  //  Calculate SSE for Y, U and V.
  if (p_Img->structure!=FRAME)
  {
    // Luma.
    diff_cmp[0] += compute_SSE(p_Img->pCurImg, p_Img->imgY_com, 0, 0, p_Inp->output.height, p_Inp->output.width);

    // Chroma.
    if (p_Img->yuv_format != YUV400)
    {
      diff_cmp[1] += compute_SSE(p_Img->pImgOrg[1], p_Img->imgUV_com[0], 0, 0, p_Inp->output.height_cr, p_Inp->output.width_cr);
      diff_cmp[2] += compute_SSE(p_Img->pImgOrg[2], p_Img->imgUV_com[1], 0, 0, p_Inp->output.height_cr, p_Inp->output.width_cr);
    }
  }
  else
  {
    if( IS_INDEPENDENT(p_Inp) )
    {
      p_Img->enc_picture = p_Img->enc_frame_picture[0];     
    }
    p_Img->pCurImg   = imgData->frm_data[0];
    p_Img->pImgOrg[0] = imgData->frm_data[0];

    // Luma.
    diff_cmp[0] += compute_SSE(p_Img->pImgOrg[0], p_Img->enc_picture->imgY, 0, 0, p_Inp->output.height, p_Inp->output.width);

    // Chroma.
    if (p_Img->yuv_format != YUV400)
    {
      p_Img->pImgOrg[1] = imgData->frm_data[1];
      p_Img->pImgOrg[2] = imgData->frm_data[2]; 

      diff_cmp[1] += compute_SSE(p_Img->pImgOrg[1], p_Img->enc_picture->imgUV[0], 0, 0, p_Inp->output.height_cr, p_Inp->output.width_cr);
      diff_cmp[2] += compute_SSE(p_Img->pImgOrg[2], p_Img->enc_picture->imgUV[1], 0, 0, p_Inp->output.height_cr, p_Inp->output.width_cr);
    }
  }

  // This should be assigned to the SSE structure. Should double check code.
  p_Dist->metric[SSE].value[0] = (float) diff_cmp[0];
  p_Dist->metric[SSE].value[1] = (float) diff_cmp[1];
  p_Dist->metric[SSE].value[2] = (float) diff_cmp[2];
}
コード例 #2
0
ファイル: md_distortion.c プロジェクト: Cheng-Hsu/JM
/*!
*************************************************************************************
* \brief
*    SSE distortion calculation for a macroblock
*************************************************************************************
*/
int64 distortionSSE(Macroblock *currMB) 
{
  ImageParameters *p_Img = currMB->p_Img;
  InputParameters *p_Inp = currMB->p_Inp;
  int64 distortionY = 0;
  int64 distortionCr[2] = {0, 0};

  // LUMA
  distortionY = compute_SSE16x16(&p_Img->pCurImg[currMB->opix_y], &p_Img->enc_picture->p_curr_img[currMB->pix_y], currMB->pix_x, currMB->pix_x);

  // CHROMA
  if ((p_Img->yuv_format != YUV400) && !IS_INDEPENDENT(p_Inp))
  {
    distortionCr[0] = compute_SSE_cr(&p_Img->pImgOrg[1][currMB->opix_c_y], &p_Img->enc_picture->imgUV[0][currMB->pix_c_y], currMB->pix_c_x, currMB->pix_c_x, p_Img->mb_cr_size_y, p_Img->mb_cr_size_x);
    distortionCr[1] = compute_SSE_cr(&p_Img->pImgOrg[2][currMB->opix_c_y], &p_Img->enc_picture->imgUV[1][currMB->pix_c_y], currMB->pix_c_x, currMB->pix_c_x, p_Img->mb_cr_size_y, p_Img->mb_cr_size_x);
  }

  return (int64)( distortionY * p_Inp->WeightY + distortionCr[0] * p_Inp->WeightCb + distortionCr[1] * p_Inp->WeightCr );
}
コード例 #3
0
ファイル: img_distortion.c プロジェクト: Cheng-Hsu/JM
void select_img(ImageParameters *p_Img, InputParameters *p_Inp, ImageStructure *imgSRC, ImageStructure *imgREF, ImageData *imgData)
{
  if (p_Img->fld_flag != FALSE)
  {
    imgSRC->format = p_Inp->output;
    imgREF->format = p_Inp->output;

    imgREF->data[0] = p_Img->pCurImg;
    imgSRC->data[0] = p_Img->imgY_com;

    if (p_Img->yuv_format != YUV400)
    {
      imgREF->data[1] = p_Img->pImgOrg[1];
      imgREF->data[2] = p_Img->pImgOrg[2];
      imgSRC->data[1] = p_Img->imgUV_com[0];
      imgSRC->data[2] = p_Img->imgUV_com[1];
    }
  }
  else
  {
    imgSRC->format = p_Inp->output;
    imgREF->format = p_Inp->output;

    imgREF->data[0] = imgData->frm_data[0];

    if ((p_Inp->PicInterlace == ADAPTIVE_CODING) || IS_INDEPENDENT(p_Inp))
    {
      p_Img->enc_picture = p_Img->enc_frame_picture[0];
    }
    imgSRC->data[0] = p_Img->enc_picture->imgY;

    if (p_Img->yuv_format != YUV400)
    {
      imgREF->data[1] = imgData->frm_data[1];
      imgREF->data[2] = imgData->frm_data[2];

      imgSRC->data[1] = p_Img->enc_picture->imgUV[0];
      imgSRC->data[2] = p_Img->enc_picture->imgUV[1];
    }
  }
}
コード例 #4
0
/*!
 ************************************************************************
 * \brief
 *    Calculate the quantisation offset parameters
 *
 ************************************************************************
*/
void CalculateOffset8Param ()
{
  int i, j, k, temp;
  int q_bits, qp;

  int max_qp_scale = imax(img->bitdepth_luma_qp_scale, img->bitdepth_chroma_qp_scale);
  int max_qp = 51 + max_qp_scale;

  if (img->type == I_SLICE || img->type == SI_SLICE )
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      q_bits = Q_BITS_8 + qp_per_matrix[qp] - OffsetBits;
      k = params->AdaptRoundingFixed ? 0 : qp;
      for (j = 0; j < 8; j++)
      {
        temp = (j << 3);
        for (i = 0; i < 8; i++)
        {          
          // INTRA8X8
          LevelOffset8x8Comp[0][1][qp][j][i] = (int) OffsetList8x8[k][0][temp] << q_bits;

          // INTRA8X8 CHROMAU
          LevelOffset8x8Comp[1][1][qp][j][i] = (int) OffsetList8x8[k][5][temp] << q_bits;

          // INTRA8X8 CHROMAV
          LevelOffset8x8Comp[2][1][qp][j][i] = (int) OffsetList8x8[k][10][temp++] << q_bits;
        }
      }
    }
  }
  else if ((img->type == P_SLICE) || (img->type == SP_SLICE))
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      q_bits = Q_BITS_8 + qp_per_matrix[qp] - OffsetBits;
      k = params->AdaptRoundingFixed ? 0 : qp;
      for (j = 0; j < 8; j++)
      {
        temp = (j << 3);
        for (i = 0; i < 8; i++)
        {
          // INTRA8X8
          LevelOffset8x8Comp[0][1][qp][j][i] = (int) OffsetList8x8[k][1][temp] << q_bits;

          // INTER8X8
          LevelOffset8x8Comp[0][0][qp][j][i] = (int) OffsetList8x8[k][3][temp] << q_bits;

          // INTRA8X8 CHROMAU
          LevelOffset8x8Comp[1][1][qp][j][i] = (int) OffsetList8x8[k][6][temp] << q_bits;

          // INTER8X8 CHROMAU
          LevelOffset8x8Comp[1][0][qp][j][i] = (int) OffsetList8x8[k][8][temp] << q_bits;

          // INTRA8X8 CHROMAV
          LevelOffset8x8Comp[2][1][qp][j][i] = (int) OffsetList8x8[k][11][temp] << q_bits;

          // INTER8X8 CHROMAV
          LevelOffset8x8Comp[2][0][qp][j][i] = (int) OffsetList8x8[k][13][temp++] << q_bits;
        }
      }
    }
  }
  else
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      q_bits = Q_BITS_8 + qp_per_matrix[qp] - OffsetBits;
      k = params->AdaptRoundingFixed ? 0 : qp;
      for (j = 0; j < 8; j++)
      {
        temp = (j << 3);
        for (i = 0; i < 8; i++)
        {
          // INTRA8X8
          LevelOffset8x8Comp[0][1][qp][j][i] = (int) OffsetList8x8[k][2][temp] << q_bits;
          // INTER8X8
          LevelOffset8x8Comp[0][0][qp][j][i] = (int) OffsetList8x8[k][4][temp] << q_bits;

          // INTRA8X8 CHROMAU
          LevelOffset8x8Comp[1][1][qp][j][i] = (int) OffsetList8x8[k][7][temp] << q_bits;

          // INTER8X8 CHROMAU
          LevelOffset8x8Comp[1][0][qp][j][i] = (int) OffsetList8x8[k][9][temp] << q_bits;

          // INTRA8X8 CHROMAV
          LevelOffset8x8Comp[2][1][qp][j][i] = (int) OffsetList8x8[k][12][temp] << q_bits;

          // INTER8X8 CHROMAV
          LevelOffset8x8Comp[2][0][qp][j][i] = (int) OffsetList8x8[k][14][temp++] << q_bits;
        }
      }
    }
  }

  // setting for 8x8 luma quantization offset
  if( IS_INDEPENDENT(params) )
  {
    if( img->colour_plane_id == 0 )
    {
      ptLevelOffset8x8 = LevelOffset8x8Comp[0];
    }
    else if( img->colour_plane_id == 1 )
    {
      ptLevelOffset8x8 = LevelOffset8x8Comp[1];
    }
    else if( img->colour_plane_id == 2 )
    {
      ptLevelOffset8x8 = LevelOffset8x8Comp[2];
    }
  }
  else
  {
    ptLevelOffset8x8 = LevelOffset8x8Comp[0];
  }
}
コード例 #5
0
/*!
 ************************************************************************
 * \brief
 *    Calculation of the quantization offset params at the frame level
 *
 * \par Input:
 *    none
 *
 * \par Output:
 *    none
 ************************************************************************
 */
void CalculateOffsetParam ()
{
  int i, j, k, temp;  
  int qp_per, qp;
  short **OffsetList;
  static int **LevelOffsetCmp0Intra, **LevelOffsetCmp1Intra, **LevelOffsetCmp2Intra;
  static int **LevelOffsetCmp0Inter, **LevelOffsetCmp1Inter, **LevelOffsetCmp2Inter;
  int img_type = (img->type == SI_SLICE ? I_SLICE : (img->type == SP_SLICE ? P_SLICE : img->type));

  int max_qp_scale = imax(img->bitdepth_luma_qp_scale, img->bitdepth_chroma_qp_scale);
  int max_qp = 51 + max_qp_scale;

  AdaptRndWeight = params->AdaptRndWFactor[img->nal_reference_idc != 0][img_type];
  AdaptRndCrWeight = params->AdaptRndCrWFactor[img->nal_reference_idc != 0][img_type];

  if (img_type == I_SLICE )
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      k = qp_per_matrix [qp];
      qp_per = Q_BITS + k - OffsetBits;
      OffsetList = OffsetList4x4[params->AdaptRoundingFixed ? 0 : qp];
      LevelOffsetCmp0Intra = LevelOffset4x4Comp[0][1][qp];
      LevelOffsetCmp1Intra = LevelOffset4x4Comp[1][1][qp];
      LevelOffsetCmp2Intra = LevelOffset4x4Comp[2][1][qp];

      temp = 0;
      for (j = 0; j < 4; j++)
      {
        for (i = 0; i < 4; i++, temp++)
        {
          LevelOffsetCmp0Intra[j][i] = (int) OffsetList[0][temp] << qp_per;
          LevelOffsetCmp1Intra[j][i] = (int) OffsetList[1][temp] << qp_per;
          LevelOffsetCmp2Intra[j][i] = (int) OffsetList[2][temp] << qp_per;
        }
      }
    }
  }
  else if (img_type == B_SLICE)
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      k = qp_per_matrix [qp];
      qp_per = Q_BITS + k - OffsetBits;
      OffsetList = OffsetList4x4[params->AdaptRoundingFixed ? 0 : qp];
      LevelOffsetCmp0Intra = LevelOffset4x4Comp[0][1][qp];
      LevelOffsetCmp1Intra = LevelOffset4x4Comp[1][1][qp];
      LevelOffsetCmp2Intra = LevelOffset4x4Comp[2][1][qp];
      LevelOffsetCmp0Inter = LevelOffset4x4Comp[0][0][qp];
      LevelOffsetCmp1Inter = LevelOffset4x4Comp[1][0][qp];
      LevelOffsetCmp2Inter = LevelOffset4x4Comp[2][0][qp];


      for (temp = 0, j = 0; j < 4; j++)
      {
        for (i = 0; i < 4; i++, temp++)
        {          
          // intra
          LevelOffsetCmp0Intra[j][i] = (int) OffsetList[6][temp] << qp_per;
          LevelOffsetCmp1Intra[j][i] = (int) OffsetList[7][temp] << qp_per;
          LevelOffsetCmp2Intra[j][i] = (int) OffsetList[8][temp] << qp_per;
        }
      }

      for (temp = 0, j = 0; j < 4; j++)
      {
        for (i = 0; i < 4; i++, temp++)
        {          
          // inter
          LevelOffsetCmp0Inter[j][i] = (int) OffsetList[12][temp] << qp_per;
          LevelOffsetCmp1Inter[j][i] = (int) OffsetList[13][temp] << qp_per;
          LevelOffsetCmp2Inter[j][i] = (int) OffsetList[14][temp] << qp_per;
        }
      }

    }
  }
  else
  {
    for (qp = 0; qp < max_qp + 1; qp++)
    {
      k = qp_per_matrix [qp];
      qp_per = Q_BITS + k - OffsetBits;
      OffsetList = OffsetList4x4[params->AdaptRoundingFixed ? 0 : qp];
      LevelOffsetCmp0Intra = LevelOffset4x4Comp[0][1][qp];
      LevelOffsetCmp1Intra = LevelOffset4x4Comp[1][1][qp];
      LevelOffsetCmp2Intra = LevelOffset4x4Comp[2][1][qp];
      LevelOffsetCmp0Inter = LevelOffset4x4Comp[0][0][qp];
      LevelOffsetCmp1Inter = LevelOffset4x4Comp[1][0][qp];
      LevelOffsetCmp2Inter = LevelOffset4x4Comp[2][0][qp];

      temp = 0;
      for (j = 0; j < 4; j++)
      {
        for (i = 0; i < 4; i++, temp++)
        {
          // intra
          LevelOffsetCmp0Intra[j][i] = (int) OffsetList[3][temp] << qp_per;
          LevelOffsetCmp1Intra[j][i] = (int) OffsetList[4][temp] << qp_per;
          LevelOffsetCmp2Intra[j][i] = (int) OffsetList[5][temp] << qp_per;
          // inter
          LevelOffsetCmp0Inter[j][i] = (int) OffsetList[9 ][temp] << qp_per;
          LevelOffsetCmp1Inter[j][i] = (int) OffsetList[10][temp] << qp_per;
          LevelOffsetCmp2Inter[j][i] = (int) OffsetList[11][temp] << qp_per;
        }
      }      
    }
  }

  // setting for 4x4 luma quantization offset
  if( IS_INDEPENDENT(params) )
  {
    if( img->colour_plane_id == 0 )
    {
      ptLevelOffset4x4 = LevelOffset4x4Comp[0];
    }
    else if( img->colour_plane_id == 1 )
    {
      ptLevelOffset4x4   = LevelOffset4x4Comp[1];
    }
    else if( img->colour_plane_id == 2 )
    {
      ptLevelOffset4x4   = LevelOffset4x4Comp[2];
    }
  }
  else
  {
    ptLevelOffset4x4 = LevelOffset4x4Comp[0];
  }
}
コード例 #6
0
ファイル: read_arc.c プロジェクト: cosyverif/deb-greatspn
static void order_nodes( Node_p start,  int type)
 {/* Init order_nodes */
  int i,j;
  Node_p point[8];
  Node_p node_pt=NULL, nxt_pt=NULL;

  for(i=0;i<8;i++) point[i]=NULL;
  for(node_pt=start;node_pt!=NULL;node_pt=nxt_pt)
   { 
    nxt_pt=node_pt->next;
    if(IS_INDEPENDENT(node_pt))
     {/* Posto neutro o con S */
      if(point[6]==NULL)
      {  
       point[6]=point[7]=node_pt;
       point[7]->next=NULL;
      }  
     else
      {  
       node_pt->next=point[6];
       point[6]=node_pt;
      }  
     }/* Posto neutro o con S */
    else
     {/* Posto normale */
      switch(node_pt->type)
       { 
	case PROJECTION :   if(point[0]==NULL)
                             {
                              point[0]=point[1]=node_pt;
                              point[1]->next=NULL;
                             }
                            else
                             {
                              node_pt->next=point[0];
                              point[0]=node_pt;
                             }
                            break;
	case COMPLEX :	    if(point[2]==NULL)
                             {
                              point[2]=point[3]=node_pt;
                              point[3]->next=NULL;
                             }
                            else
                             {
                              node_pt->next=point[2];
                              point[2]=node_pt;
                             }
                            break;
	default :	    if(point[4]==NULL)
                             {
                              point[4]=point[5]=node_pt;
                              point[5]->next=NULL;
                             }
                            else
                             {
                              node_pt->next=point[4];
                              point[4]=node_pt;
                             }
                            break;
       }
     }/* Posto normale */
   }
  for(i=0;i<8;)
    if(point[i]!=NULL)
     {
      for(j=i+2; j<8; j+=2)
       if(point[j]!=NULL)
        break;
      if(j<8)
       point[i+1]->next = point[j];
      else
       point[i+1]->next = NULL;
      i = j;
     }
    else
     i+=2;
  for(i=0;i<8;i+=2)
   if(point[i]!=NULL)
    {
     node_pt = point[i];
     break;
    }
  switch(type)
   {
    case INHIBITOR : tabt[ntr].inibptr=node_pt;
		     break;
    case INPUT :     tabt[ntr].inptr=node_pt;
		     break;
    case OUTPUT :    tabt[ntr].outptr=node_pt;
		     break;
   }
 }/* End order_nodes */
コード例 #7
0
/*!
*************************************************************************************
* \brief
*    Mode Decision for a macroblock with error resilience
*************************************************************************************
*/
void encode_one_macroblock_highloss (Macroblock *currMB)
{
  int         max_index = 9;
  int         rerun, block, index, mode, i, j, ctr16x16;
  char        best_pdir;
  RD_PARAMS   enc_mb;
  double      min_rdcost = 1e30;
  double      min_dcost = 1e30;
  char        best_ref[2] = {0, -1};
  int         bmcost[5] = {INT_MAX};
  int         cost=0;
  int         min_cost = INT_MAX, cost_direct=0, have_direct=0, i16mode=0;
  int         intra1 = 0;
  int         cost8x8_direct = 0;
  int         mb_available_up;
  int         mb_available_left;
  int         mb_available_up_left;
  int         best8x8l0ref, best8x8l1ref; 
  int         is_cavlc = (img->currentSlice->symbol_mode == CAVLC);

  short       islice      = (short) (img->type==I_SLICE);
  short       bslice      = (short) (img->type==B_SLICE);
  short       pslice      = (short) ((img->type==P_SLICE) || (img->type==SP_SLICE));
  short       intra       = (short) (islice || (pslice && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra));
  int         lambda_mf[3];
  short       runs        = (short) (params->RestrictRef==1 && (pslice  || (bslice && img->nal_reference_idc>0)) ? 2 : 1);

  int         prev_mb_nr  = FmoGetPreviousMBNr(img->current_mb_nr);
  Macroblock* prevMB      = (prev_mb_nr >= 0) ? &img->mb_data[prev_mb_nr]:NULL ;
  imgpel  (*mb_pred)[16] = img->mb_pred[0];
  Block8x8Info *b8x8info   = img->b8x8info;

  short   min_chroma_pred_mode, max_chroma_pred_mode;

  short   inter_skip = 0;
  short   bipred_me = 0;
  double  min_rate = 0;

  if(params->SearchMode == UM_HEX)
  {
    UMHEX_decide_intrabk_SAD();
  }
  else if (params->SearchMode == UM_HEX_SIMPLE)
  {
    smpUMHEX_decide_intrabk_SAD();
  }

  intra |= RandomIntra (img->current_mb_nr);    // Forced Pseudo-Random Intra

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

  // Perform multiple encodings if rdopt with losses is enabled
  for (rerun=0; rerun<runs; rerun++)
  {
    if (runs==2)
      params->rdopt= (rerun==0) ? 1 : 3;

    // reset chroma intra predictor to default
    currMB->c_ipred_mode = DC_PRED_8;

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

    if (!intra)
    {
      //===== set direct motion vectors =====
      best_mode = 1;
      if (bslice)
      {
        Get_Direct_Motion_Vectors (currMB);
      }

      if (params->CtxAdptLagrangeMult == 1)
      {
        get_initial_mb16x16_cost(currMB);
      }

      //===== MOTION ESTIMATION FOR 16x16, 16x8, 8x16 BLOCKS =====
      for (min_cost=INT_MAX, mode=1; mode<4; mode++)
      {
        bipred_me = 0;
        b8x8info->bipred8x8me[mode][0] = 0;
        if (enc_mb.valid[mode])
        {
          for (cost=0, block=0; block<(mode==1?1:2); block++)
          {
            update_lambda_costs(&enc_mb, lambda_mf);
            PartitionMotionSearch (currMB, mode, block, lambda_mf);

            //--- set 4x4 block indizes (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] = INT_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] = INT_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 prediction ME is only supported for modes 1, 2, 3
              if (is_bipred_enabled(mode))
              {
                list_prediction_cost(currMB, BI_PRED_L0, block, mode, &enc_mb, bmcost, 0);
                list_prediction_cost(currMB, BI_PRED_L1, block, mode, &enc_mb, bmcost, 0);
              }
              else
              {
                bmcost[BI_PRED_L0] = INT_MAX;
                bmcost[BI_PRED_L1] = INT_MAX;
              }

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

            assign_enc_picture_params(mode, best_pdir, block, enc_mb.list_offset[LIST_0], best_ref[LIST_0], best_ref[LIST_1], bslice, bipred_me);
            //----- set reference frame and direction parameters -----
            set_block8x8_info(b8x8info, mode, block, best_ref, best_pdir, bipred_me);
            
            
            //--- set reference frames and motion vectors ---
            if (mode>1 && block==0)
              SetRefAndMotionVectors (currMB, block, mode, best_pdir, best_ref[LIST_0], best_ref[LIST_1], bipred_me);
          } // for (block=0; block<(mode==1?1:2); block++)

          if (cost < min_cost)
          {
            best_mode = (short) mode;
            min_cost  = cost;
            if (params->CtxAdptLagrangeMult == 1)
            {
              adjust_mb16x16_cost(cost);
            }
          }
        } // if (enc_mb.valid[mode])
      } // for (mode=1; mode<4; mode++)

    if (enc_mb.valid[P8x8])
      {
        giRDOpt_B8OnlyFlag = 1;

        tr8x8.mb_p8x8_cost = INT_MAX;
        tr4x4.mb_p8x8_cost = INT_MAX;
        //===== store coding state of macroblock =====
        store_coding_state (currMB, cs_mb);

        currMB->all_blk_8x8 = -1;

        if (params->Transform8x8Mode)
        {
          tr8x8.mb_p8x8_cost = 0;
          //===========================================================
          // Check 8x8 partition with transform size 8x8
          //===========================================================
          //=====  LOOP OVER 8x8 SUB-PARTITIONS  (Motion Estimation & Mode Decision) =====
          for (cost_direct=cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
          {
            submacroblock_mode_decision(&enc_mb, &tr8x8, currMB, cofAC8x8ts[0][block], cofAC8x8ts[1][block], cofAC8x8ts[2][block],
              &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 1, is_cavlc);
            set_subblock8x8_info(b8x8info, P8x8, block, &tr8x8);
          }

          // following params could be added in RD_8x8DATA structure
          cbp8_8x8ts      = cbp8x8;
          cbp_blk8_8x8ts  = cbp_blk8x8;
          cnt_nonz8_8x8ts = cnt_nonz_8x8;
          currMB->luma_transform_size_8x8_flag = 0; //switch to 4x4 transform size

          //--- re-set coding state (as it was before 8x8 block coding) ---
          //reset_coding_state (currMB, cs_mb);
        }// if (params->Transform8x8Mode)


        if (params->Transform8x8Mode != 2)
        {
          tr4x4.mb_p8x8_cost = 0;
          //=================================================================
          // Check 8x8, 8x4, 4x8 and 4x4 partitions with transform size 4x4
          //=================================================================
          //=====  LOOP OVER 8x8 SUB-PARTITIONS  (Motion Estimation & Mode Decision) =====
          for (cost_direct=cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
          {
            submacroblock_mode_decision(&enc_mb, &tr4x4, currMB, cofAC8x8[block], cofAC8x8CbCr[0][block], cofAC8x8CbCr[1][block],
              &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 0, is_cavlc);
            set_subblock8x8_info(b8x8info, P8x8, block, &tr4x4);
          }
          //--- re-set coding state (as it was before 8x8 block coding) ---
          // reset_coding_state (currMB, cs_mb);
        }// if (params->Transform8x8Mode != 2)

        //--- re-set coding state (as it was before 8x8 block coding) ---
        reset_coding_state (currMB, cs_mb);

        // This is not enabled yet since mpr has reverse order.
        if (params->RCEnable)
          rc_store_diff(img->opix_x, img->opix_y, mb_pred);

        //check cost for P8x8 for non-rdopt mode
        giRDOpt_B8OnlyFlag = 0;
      }
      else // if (enc_mb.valid[P8x8])
      {
        tr4x4.mb_p8x8_cost = INT_MAX;
      }

      // Find a motion vector for the Skip mode
    if(pslice)
        FindSkipModeMotionVector (currMB);
    }
    else // if (!intra)
    {
      min_cost = INT_MAX;
    }

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

   {
     if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
     {
       // precompute all new chroma intra prediction modes
       IntraChromaPrediction(currMB, &mb_available_up, &mb_available_left, &mb_available_up_left);

       if (params->FastCrIntraDecision )
       {
         IntraChromaRDDecision(currMB, enc_mb);
         min_chroma_pred_mode = (short) currMB->c_ipred_mode;
         max_chroma_pred_mode = (short) currMB->c_ipred_mode;
       }
       else
       {
         min_chroma_pred_mode = DC_PRED_8;
         max_chroma_pred_mode = PLANE_8;
       }
     }
     else
     {
       min_chroma_pred_mode = DC_PRED_8;
       max_chroma_pred_mode = DC_PRED_8;
     }

     for (currMB->c_ipred_mode=min_chroma_pred_mode; currMB->c_ipred_mode<=max_chroma_pred_mode; currMB->c_ipred_mode++)
     {
       // bypass if c_ipred_mode is not allowed
       if ( (img->yuv_format != YUV400) &&
         (  ((!intra || !params->IntraDisableInterOnly) && params->ChromaIntraDisable == 1 && currMB->c_ipred_mode!=DC_PRED_8)
         || (currMB->c_ipred_mode == VERT_PRED_8 && !mb_available_up)
         || (currMB->c_ipred_mode == HOR_PRED_8  && !mb_available_left)
         || (currMB->c_ipred_mode == PLANE_8     && (!mb_available_left || !mb_available_up || !mb_available_up_left))))
         continue;

       //===== GET BEST MACROBLOCK MODE =====
       for (ctr16x16=0, index=0; index < max_index; index++)
       {
         mode = mb_mode_table[index];

         if (img->yuv_format != YUV400)
         {
           i16mode = 0;
         }
         //--- for INTER16x16 check all prediction directions ---
         if (mode==1 && bslice)
         {
           update_prediction_for_mode16x16(b8x8info, ctr16x16, &index);
           ctr16x16++;
         }

         // Skip intra modes in inter slices if best mode is inter <P8x8 with cbp equal to 0
         if (params->SkipIntraInInterSlices && !intra && mode >= I4MB && best_mode <=3 && currMB->cbp == 0)
           continue;

      // check if weights are in valid range for biprediction.
         if (bslice && active_pps->weighted_bipred_idc == 1 && mode < P8x8)
         {
           int cur_blk, cur_comp;
           int weight_sum;
           Boolean invalid_mode = FALSE;
           for (cur_blk = 0; cur_blk < 4; cur_blk ++)
           {
             if (b8x8info->best8x8pdir[mode][cur_blk] == 2)
             {
               for (cur_comp = 0; cur_comp < (active_sps->chroma_format_idc == YUV400 ? 1 : 3) ; cur_comp ++)
               {
                 best8x8l0ref = (int) b8x8info->best8x8l0ref[mode][cur_blk];
                 best8x8l1ref = (int) b8x8info->best8x8l1ref[mode][cur_blk];
                 weight_sum = wbp_weight[0][best8x8l0ref][best8x8l1ref][cur_comp] + wbp_weight[1][best8x8l0ref][best8x8l1ref][cur_comp];

                 if (weight_sum < -128 ||  weight_sum > 127)
                 {
                   invalid_mode = TRUE;
                   break;
                 }
               }
               if (invalid_mode == TRUE)
                 break;
             }
           }
           if (invalid_mode == TRUE)
             continue;
         }

         if (enc_mb.valid[mode])
           compute_mode_RD_cost(mode, currMB, &enc_mb, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);

       }// for (ctr16x16=0, index=0; index<max_index; index++)
     }// for (currMB->c_ipred_mode=DC_PRED_8; currMB->c_ipred_mode<=max_chroma_pred_mode; currMB->c_ipred_mode++)

#ifdef BEST_NZ_COEFF
     for (j=0;j<4;j++)
       for (i=0; i<(4+img->num_blk8x8_uv); i++)
         img->nz_coeff[img->current_mb_nr][j][i] = gaaiMBAFF_NZCoeff[j][i];
#endif
   }

   if (rerun==0)
     intra1 = IS_INTRA(currMB);
  } // for (rerun=0; rerun<runs; rerun++)

  //=====  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 ======
  //---------------------------------------------------------------------------

  update_qp_cbp_tmp(currMB, cbp, best_mode);
  set_stored_macroblock_parameters (currMB);

  // Rate control
  if(params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE)
    rc_store_mad(currMB);
  update_qp_cbp(currMB, best_mode);

  rdopt->min_rdcost = min_rdcost;
  rdopt->min_dcost = min_dcost;

  if ( (img->MbaffFrameFlag)
    && (img->current_mb_nr%2)
    && (currMB->mb_type ? 0:((bslice) ? !currMB->cbp:1))  // bottom is skip
    && (prevMB->mb_type ? 0:((bslice) ? !prevMB->cbp:1))
    && !(field_flag_inference(currMB) == enc_mb.curr_mb_field)) // top is skip
  {
    rdopt->min_rdcost = 1e30;  // don't allow coding of a MB pair as skip if wrong inference
  }

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

  if(params->SearchMode == UM_HEX)
  {
    UMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
  }
  else if(params->SearchMode == UM_HEX_SIMPLE)
  {
    smpUMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
  }

  //--- constrain intra prediction ---
  if(params->UseConstrainedIntraPred && (img->type==P_SLICE || img->type==B_SLICE))
  {
    img->intra_block[img->current_mb_nr] = IS_INTRA(currMB);
  }
}