⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 me_umhexsmp.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 4 页
字号:
  short blocksize_y     = (short) params->blc_size[blocktype][1];
  int   pic4_pix_x      = ((pic_pix_x + IMG_PAD_SIZE)<<2);
  int   pic4_pix_y      = ((pic_pix_y + IMG_PAD_SIZE)<<2);
  short max_pos_x4      = (short) ((ref_picture->size_x - blocksize_x + 2*IMG_PAD_SIZE)<<2);
  short max_pos_y4      = (short) ((ref_picture->size_y - blocksize_y + 2*IMG_PAD_SIZE)<<2);

  int   iXMinNow, iYMinNow;
  short dynamic_search_range, i, m;
  int   currmv_x = 0, currmv_y = 0;
  int   pred_frac_mv_x,pred_frac_mv_y,abort_search;
  int   pred_frac_up_mv_x, pred_frac_up_mv_y;

  dist_method = Q_PEL + 3 * apply_weights;

  ref_pic_sub.luma = ref_picture->p_curr_img_sub;

  img_width  = ref_pic_ptr->size_x;
  img_height = ref_pic_ptr->size_y;
  width_pad  = ref_pic_ptr->size_x_pad;
  height_pad = ref_pic_ptr->size_y_pad;

  if (apply_weights)
  {
    weight_luma = wp_weight[list + list_offset][ref][0];
    offset_luma = wp_offset[list + list_offset][ref][0];
  }

  if (ChromaMEEnable)
  {
    ref_pic_sub.crcb[0] = ref_pic_ptr->imgUV_sub[0];
    ref_pic_sub.crcb[1] = ref_pic_ptr->imgUV_sub[1];
    width_pad_cr  = ref_pic_ptr->size_x_cr_pad;
    height_pad_cr = ref_pic_ptr->size_y_cr_pad;

    if (apply_weights)
    {
      weight_cr[0] = wp_weight[list + list_offset][ref][1];
      weight_cr[1] = wp_weight[list + list_offset][ref][2];
      offset_cr[0] = wp_offset[list + list_offset][ref][1];
      offset_cr[1] = wp_offset[list + list_offset][ref][2];
    }
  }


  if ((pic4_pix_x + mv[0] > 1) && (pic4_pix_x + mv[0] < max_pos_x4 - 1) &&
    (pic4_pix_y + mv[1] > 1) && (pic4_pix_y + mv[1] < max_pos_y4 - 1))
  {
    ref_access_method = FAST_ACCESS;
  }
  else
  {
    ref_access_method = UMV_ACCESS;
  }

  dynamic_search_range = 3;
  pred_frac_mv_x = (pred_mv[0] - mv[0]) % 4;
  pred_frac_mv_y = (pred_mv[1] - mv[1]) % 4;

  pred_frac_up_mv_x = (smpUMHEX_pred_MV_uplayer_X - mv[0]) % 4;
  pred_frac_up_mv_y = (smpUMHEX_pred_MV_uplayer_Y - mv[1]) % 4;

  memset(smpUMHEX_SearchState[0], 0,
    (2*dynamic_search_range+1)*(2*dynamic_search_range+1));

  smpUMHEX_SearchState[dynamic_search_range][dynamic_search_range] = 1;
  if( !start_me_refinement_hp )
  {
    cand_mv_x = mv[0];
    cand_mv_y = mv[1];
    mcost   = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv[0], pred_mv[1]);
    mcost   += computeUniPred[dist_method]( orig_pic, blocksize_y, blocksize_x,
      min_mcost - mcost, cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y);
    if (mcost < min_mcost)
    {
      min_mcost = mcost;
      currmv_x  = cand_mv_x;
      currmv_y  = cand_mv_y;
    }
  }
  else
  {
    currmv_x = mv[0];
    currmv_y = mv[1];
  }

  // If the min_mcost is small enough and other statistics are positive,
  // better to stop the search now
  if ( ((mv[0]) == 0) && ((mv[1]) == 0) &&
    (pred_frac_mv_x == 0 && pred_frac_up_mv_x == 0) &&
    (pred_frac_mv_y == 0 && pred_frac_up_mv_y == 0) &&
    (min_mcost < (SubPelThreshold1>>block_type_shift_factor[blocktype])) )
  {
    mv[0] = (short) currmv_x;
    mv[1] = (short) currmv_y;
    return min_mcost;
  }

  if(pred_frac_mv_x || pred_frac_mv_y)
  {
    cand_mv_x = mv[0] + pred_frac_mv_x;
    cand_mv_y = mv[1] + pred_frac_mv_y;
    mcost   = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv[0], pred_mv[1]);
    mcost   += computeUniPred[dist_method]( orig_pic, blocksize_y, blocksize_x,
      min_mcost - mcost, cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y);

    smpUMHEX_SearchState[cand_mv_y -mv[1] + dynamic_search_range][cand_mv_x - mv[0] + dynamic_search_range] = 1;
    if (mcost < min_mcost)
    {
      min_mcost = mcost;
      currmv_x  = cand_mv_x;
      currmv_y  = cand_mv_y;
    }
  }

  // Multiple small diamond search
  for(i = 0; i < dynamic_search_range; i++)
  {
    abort_search = 1;

    iXMinNow = currmv_x;
    iYMinNow = currmv_y;
    for (m = 0; m < 4; m++)
    {
      cand_mv_x = iXMinNow + Diamond_X[m];
      cand_mv_y = iYMinNow + Diamond_Y[m];

      if(iabs(cand_mv_x - mv[0]) <= dynamic_search_range && iabs(cand_mv_y - mv[1]) <= dynamic_search_range)
      {
        if(!smpUMHEX_SearchState[cand_mv_y - mv[1] + dynamic_search_range][cand_mv_x - mv[0] + dynamic_search_range])
        {
          mcost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv[0], pred_mv[1]);
          mcost += computeUniPred[dist_method]( orig_pic, blocksize_y, blocksize_x,
            min_mcost - mcost, cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y);

          smpUMHEX_SearchState[cand_mv_y - mv[1] + dynamic_search_range][cand_mv_x - mv[0] + dynamic_search_range] = 1;

          if (mcost < min_mcost)
          {
            min_mcost    = mcost;
            currmv_x     = cand_mv_x;
            currmv_y     = cand_mv_y;
            abort_search = 0;
          }
          if (min_mcost < (SubPelThreshold3>>block_type_shift_factor[blocktype]))
          {
            mv[0] = (short) currmv_x;
            mv[1] = (short) currmv_y;
            return min_mcost;
          }
        }
      }
    }
    // If the minimum cost point is in the center, break out the loop
    if (abort_search)
    {
      break;
    }
  }

  mv[0] = (short) currmv_x;
  mv[1] = (short) currmv_y;
  return min_mcost;
}


int                                                   //  ==> minimum motion cost after search
smpUMHEXSubPelBlockME (imgpel*   orig_pic,      // <--  original pixel values for the AxB block
                    short     ref,           // <--  reference frame (0... or -1 (backward))
                    int       list,
                    int       list_offset,   // <--  MBAFF list offset
                    int       pic_pix_x,     // <--  absolute x-coordinate of regarded AxB block
                    int       pic_pix_y,     // <--  absolute y-coordinate of regarded AxB block
                    int       blocktype,     // <--  block type (1-16x16 ... 7-4x4)
                    short     pred_mv[2],    // <--  motion vector predictor (x|y) in sub-pel units
                    short     mv[2],         // <--> in: search center (x|y) / out: motion vector (x|y) - in sub-pel units
                    int       search_pos2,   // <--  search positions for    half-pel search  (default: 9)
                    int       search_pos4,   // <--  search positions for quarter-pel search  (default: 9)
                    int       min_mcost,     // <--  minimum motion cost (cost for center or huge value)
                    int*      lambda_factor,
                    int       apply_weights
                    )
{  
  if(blocktype > 1)
  {
    min_mcost =  smpUMHEXSubPelBlockMotionSearch (orig_pic, ref, list, list_offset, pic_pix_x, pic_pix_y,
      blocktype, pred_mv, mv, 9, 9, min_mcost, lambda_factor[Q_PEL], apply_weights);
  }
  else
  {
    min_mcost =  smpUMHEXFullSubPelBlockMotionSearch (orig_pic, ref, list, list_offset, pic_pix_x, pic_pix_y,
      blocktype, pred_mv, mv, 9, 9, min_mcost, lambda_factor[Q_PEL], apply_weights);
  }

  return min_mcost;
}

/*!
 ************************************************************************
 * \brief
 *    smpUMHEXBipredIntegerPelBlockMotionSearch: fast pixel block motion search for bipred mode
 *
 ************************************************************************
 */
int                                                           //  ==> minimum motion cost after search
smpUMHEXBipredIntegerPelBlockMotionSearch (Macroblock *currMB, // <--  current Macroblock
                                           imgpel* cur_pic,   // <--  original pixel values for the AxB block
                                           short     ref,           // <--  reference frame (0... or -1 (backward))
                                           int       list,          // <--  Current reference list
                                           int       list_offset,   // <--  MBAFF list offset
                                           char   ***refPic,        // <--  reference array
                                           short ****tmp_mv,        // <--  mv array
                                           int       pic_pix_x,     // <--  absolute x-coordinate of regarded AxB block
                                           int       pic_pix_y,     // <--  absolute y-coordinate of regarded AxB block
                                           int       blocktype,     // <--  block type (1-16x16 ... 7-4x4)
                                           short     pred_mv1[2],   // <--  motion vector predictor (x|y) in sub-pel units
                                           short     pred_mv2[2],   // <--  motion vector predictor (x|y) in sub-pel units
                                           short     mv[2],         // <--> in: search center (x|y) / out: motion vector (x|y) - in pel units
                                           short     s_mv[2],       // <--> in: search center (x|y) 
                                           int       search_range,  // <--  1-d search range in pel units
                                           int       min_mcost,     // <--  minimum motion cost (cost for center or huge value)
                                           int       iteration_no,  // <--  bi pred iteration number
                                           int       lambda_factor, // <--  lagrangian parameter for determining motion cost
                                           int       apply_weights
                                           )
{
  int   mvshift       = 2;                              // motion vector shift for getting sub-pel units

  int   search_step, iYMinNow, iXMinNow;
  int   i, m;
  int   cand_x, cand_y, mcost;
  int   blocksize_y   = params->blc_size[blocktype][1];  // vertical block size
  int   blocksize_x   = params->blc_size[blocktype][0];  // horizontal block size
  int   pred_x1       = (pic_pix_x << 2) + pred_mv1[0];  // predicted position x (in sub-pel units)
  int   pred_y1       = (pic_pix_y << 2) + pred_mv1[1];  // predicted position y (in sub-pel units)
  int   pred_x2       = (pic_pix_x << 2) + pred_mv2[0];  // predicted position x (in sub-pel units)
  int   pred_y2       = (pic_pix_y << 2) + pred_mv2[1];  // predicted position y (in sub-pel units)
  short center2_x     = pic_pix_x + mv[0];               // center position x (in pel units)
  short center2_y     = pic_pix_y + mv[1];               // center position y (in pel units)
  short center1_x     = pic_pix_x + s_mv[0];             // mvx of second pred (in pel units)
  short center1_y     = pic_pix_y + s_mv[1];             // mvy of second pred (in pel units)
  int   best_x        = center2_x;
  int   best_y        = center2_y;

  short offset1 = (apply_weights ? (list == 0?  wp_offset[list_offset    ][ref][0]:  wp_offset[list_offset + 1][0  ][ref]) : 0);
  short offset2 = (apply_weights ? (list == 0?  wp_offset[list_offset + 1][ref][0]:  wp_offset[list_offset    ][0  ][ref]) : 0);

  ref_pic1_sub.luma = listX[list + list_offset][ref]->p_curr_img_sub;
  ref_pic2_sub.luma = listX[list == 0 ? 1 + list_offset: list_offset][ 0 ]->p_curr_img_sub;

  img_width  = listX[list + list_offset][ref]->size_x;
  img_height = listX[list + list_offset][ref]->size_y;
  width_pad  = listX[list + list_offset][ref]->size_x_pad;
  height_pad = listX[list + list_offset][ref]->size_y_pad;

  if (apply_weights)
  {
    weight1 = list == 0 ? wbp_weight[list_offset         ][ref][0][0] : wbp_weight[list_offset + LIST_1][0  ][ref][0];
    weight2 = list == 0 ? wbp_weight[list_offset + LIST_1][ref][0][0] : wbp_weight[list_offset         ][0  ][ref][0];
    offsetBi=(offset1 + offset2 + 1)>>1;
    computeBiPred = computeBiPredSAD2; //ME only supports SAD computations
  }
  else
  {
    weight1 = 1<<luma_log_weight_denom;
    weight2 = 1<<luma_log_weight_denom;
    offsetBi = 0;
    computeBiPred = computeBiPredSAD1; //ME only supports SAD computations
  }

  if (ChromaMEEnable )
  {
    ref_pic1_sub.crcb[0] = listX[list + list_offset][ref]->imgUV_sub[0];
    ref_pic1_sub.crcb[1] = listX[list + list_offset][ref]->imgUV_sub[1];
    ref_pic2_sub.crcb[0] = listX[list == 0 ? 1 + list_offset: list_offset][ 0 ]->imgUV_sub[0];
    ref_pic2_sub.crcb[1] = listX[list == 0 ? 1 + list_offset: list_offset][ 0 ]->imgUV_sub[1];
    width_pad_cr  = listX[list + list_offset][ref]->size_x_cr_pad;
    height_pad_cr = listX[list + list_offset][ref]->size_y_cr_pad;
    if (apply_weights)
    {
      weight1_cr[0] = list == 0 ? wbp_weight[list_offset         ][ref][0][1] : wbp_weight[list_offset + LIST_1][0  ][ref][1];
      weight1_cr[1] = list == 0 ? wbp_weight[list_offset         ][ref][0][2] : wbp_weight[list_offset + LIST_1][0  ][ref][2];
      weight2_cr[0] = list == 0 ? wbp_weight[list_offset + LIST_1][ref][0][1] : wbp_weight[list_offset         ][0  ][ref][1];
      weight2_cr[1] = list == 0 ? wbp_weight[list_offset + LIST_1][ref][0][2] : wbp_weight[list_offset         ][0  ][ref][2];
      offsetBi_cr[0] = (list == 0)
        ? (wp_offset[list_offset         ][ref][1] + wp_offset[list_offset + LIST_1][ref][1] + 1) >> 1
        : (wp_offset[list_offset + LIST_1][0  ][1] + wp_offset[list_offset         ][0  ][1] + 1) >> 1;
      offsetBi_cr[1] = (list == 0)
        ? (wp_offset[list_offset         ][ref][2] + wp_offset[list_offset + LIST_1][ref][2] + 1) >> 1
        : (wp_offset[list_offset + LIST_1][0  ][2] + wp_offset[list_offset         ][0  ][2] + 1) >> 1;
    }
    else
    {
      weight1_cr[0] = 1<<chroma_log_weight_denom;
      weight1_cr[1] = 1<<chroma_log_weight_denom;
      weight2_cr[0] = 1<<chroma_log_weight_denom;
      weight2_cr[1] = 1<<chroma_log_weight_denom;
      offsetBi_cr[0] = 0;
      offsetBi_cr[1] = 0;
    }
  }

  // Set function for getting reference picture lines
  if ((center2_x > search_range) && (center2_x < img_width -1-search_range-blocksize_x) &&
      (center2_y > search_range) && (center2_y < img_height-1-search_range-blocksize_y))
  {
    bipred2_access_method = FAST_ACCESS;
  }
  else
  {
    bipred2_access_method = UMV_ACCESS;
  }

  // Set function for getting reference picture lines
  if ((center1_y > search_range) && (center1_y < img_height-1-search_range-blocksize_y))
  {
    bipred1_access_method = FAST_ACCESS;
  }
  else
  {
    bipred1_access_method = UMV_ACCESS;
  }

  // Check the center median predictor

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -