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

📄 simplified_fast_me.c

📁 H264视频编解码程序
💻 C
📖 第 1 页 / 共 2 页
字号:

    // Hexagon Search
    iXMinNow = best_x;
    iYMinNow = best_y;
    for (m = 0; m < 6; m++)
    {
      cand_x = iXMinNow + Hexagon_X[m];
      cand_y = iYMinNow + Hexagon_Y[m];
      SEARCH_ONE_PIXEL_HELPER
    }
    // Multi Big Hexagon Search
    iXMinNow = best_x;
    iYMinNow = best_y;
    for(i = 1; i <= search_range/4; i++)
    {
      for (m = 0; m < 16; m++)
      {
        cand_x = iXMinNow + Big_Hexagon_X[m]*i;
        cand_y = iYMinNow + Big_Hexagon_Y[m]*i;
        SEARCH_ONE_PIXEL_HELPER
      }
    }
  }

  // Search up_layer predictor for non 16x16 blocks
  if (blocktype > 1)
  {
    cand_x = pic_pix_x + (simplified_pred_MV_uplayer_X/4);
    cand_y = pic_pix_y + (simplified_pred_MV_uplayer_Y/4);
    SEARCH_ONE_PIXEL_HELPER
  }

  if(center_x != pic_pix_x || center_y != pic_pix_y)
  {
    cand_x = pic_pix_x;
    cand_y = pic_pix_y;
    SEARCH_ONE_PIXEL_HELPER

    iXMinNow = best_x;
    iYMinNow = best_y;
    // Local diamond search
    for (m = 0; m < 4; m++)
    {   
      cand_x = iXMinNow + Diamond_X[m];
      cand_y = iYMinNow + Diamond_Y[m];
      SEARCH_ONE_PIXEL_HELPER
    }
  }

  // If the minimum cost is small enough, do a local search
  // and finish the search here
  if (min_mcost < (ConvergeThreshold>>block_type_shift_factor[blocktype]))
  {
    iXMinNow = best_x;
    iYMinNow = best_y;
    for (m = 0; m < 4; m++)
    {   
      cand_x = iXMinNow + Diamond_X[m];
      cand_y = iYMinNow + Diamond_Y[m];
      SEARCH_ONE_PIXEL_HELPER
    }
    *mv_x = best_x - pic_pix_x;
    *mv_y = best_y - pic_pix_y; 
    return min_mcost;
  }

  //second_step:  Extended Hexagon-based Search
  for(i = 0; i < search_range; i++)
  {
    iXMinNow = best_x;
    iYMinNow = best_y;
    for (m = 0; m < 6; m++)
    {
      cand_x = iXMinNow + Hexagon_X[m];
      cand_y = iYMinNow + Hexagon_Y[m];
      SEARCH_ONE_PIXEL_HELPER
    }
    // The minimum cost point happens in the center
    if (best_x == iXMinNow && best_y == iYMinNow)
    {
      break;
    }
  }

  //third_step: Small diamond search
  for(i = 0; i < search_range; i++)
  {
    iXMinNow = best_x;
    iYMinNow = best_y;
    for (m = 0; m < 4; m++)
    {
      cand_x = iXMinNow + Diamond_X[m];
      cand_y = iYMinNow + Diamond_Y[m];
      SEARCH_ONE_PIXEL_HELPER
    }

    // The minimum cost point happens in the center
    if (best_x == iXMinNow && best_y == iYMinNow)
    {
      break;
    }
  }

  *mv_x = best_x - pic_pix_x;
  *mv_y = best_y - pic_pix_y; 
  return min_mcost;
}

/*!
 ************************************************************************
 * \brief
 *    Fast sub pixel block motion estimation 
 ************************************************************************
 */
int                                     //  ==> minimum motion cost after search
simplified_FastSubPelBlockMotionSearch (
               pel_t** orig_pic,        // <--  original pixel values for the AxB block
               short     ref,           // <--  reference frame (0... or -1 (backward))
               int       list,          // <--  reference picture list
               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_x,     // <--  motion vector predictor (x) in sub-pel units
               short     pred_mv_y,     // <--  motion vector predictor (y) in sub-pel units
               short*    mv_x,          // <--> in: search center (x) / out: MV (x) - in pel units
               short*    mv_y,          // <--> in: search center (y) / out: MV (y) - in 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, // <--  lagrangian parameter for determining motion cost
               int       useABT)
{
  int   mcost;
  int   cand_mv_x, cand_mv_y;
  
  int   list_offset     = ((img->MbaffFrameFlag) &&
                           (img->mb_data[img->current_mb_nr].mb_field)) ?
                            img->current_mb_nr%2 ? 4 : 2 : 0;
  StorablePicture *ref_picture = listX[list+list_offset][ref];
  
  short mv_shift        = 0;
  short blocksize_x     = input->blc_size[blocktype][0];
  short blocksize_y     = input->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      = ((ref_picture->size_x - blocksize_x + 2*IMG_PAD_SIZE)<<2);
  short max_pos_y4      = ((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   mv_cost;
  int   pred_frac_up_mv_x, pred_frac_up_mv_y;

  if ((pic4_pix_x + *mv_x > 1) && (pic4_pix_x + *mv_x < max_pos_x4 - 1) &&
      (pic4_pix_y + *mv_y > 1) && (pic4_pix_y + *mv_y < max_pos_y4 - 1))
  {
    get_line = FastLine4X;
  }
  else
  {
    get_line = UMVLine4X;    
  }
  
  dynamic_search_range = 3;
  pred_frac_mv_x = (pred_mv_x - *mv_x) % 4;
  pred_frac_mv_y = (pred_mv_y - *mv_y) % 4; 
  
  pred_frac_up_mv_x = (simplified_pred_MV_uplayer_X - *mv_x) % 4;
  pred_frac_up_mv_y = (simplified_pred_MV_uplayer_Y - *mv_y) % 4;

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

  simplified_SearchState[dynamic_search_range][dynamic_search_range] = 1;
  if(input->hadamard)
  {
    cand_mv_x = *mv_x;    
    cand_mv_y = *mv_y;    
    mv_cost   = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);
    mcost     = simplified_add_up_SAD_quarter_pel(pic_pix_x, pic_pix_y, blocksize_x, blocksize_y,
                                cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y,
                                ref_picture, orig_pic, mv_cost, min_mcost, useABT, blocktype);
    if (mcost < min_mcost)
    {
      min_mcost = mcost;
      currmv_x  = cand_mv_x;
      currmv_y  = cand_mv_y; 
    }
  }
  else
  {
    currmv_x = *mv_x;
    currmv_y = *mv_y; 
  }

  // If the min_mcost is small enough and other statistics are positive,
  // better to stop the search now
  if ( ((*mv_x) == 0) && ((*mv_y) == 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_x = currmv_x;
    *mv_y = currmv_y;
	return min_mcost;
  }

  if(pred_frac_mv_x || pred_frac_mv_y)
  {
    cand_mv_x = *mv_x + pred_frac_mv_x;    
    cand_mv_y = *mv_y + pred_frac_mv_y;    
    mv_cost   = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);    
    mcost     = simplified_add_up_SAD_quarter_pel(pic_pix_x, pic_pix_y, blocksize_x, blocksize_y,
                            cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y,
                            ref_picture, orig_pic,mv_cost, min_mcost, useABT, blocktype);
    simplified_SearchState[cand_mv_y -*mv_y + dynamic_search_range][cand_mv_x - *mv_x + 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(absm(cand_mv_x - *mv_x) <= dynamic_search_range && absm(cand_mv_y - *mv_y) <= dynamic_search_range)
      {
        if(!simplified_SearchState[cand_mv_y - *mv_y + dynamic_search_range][cand_mv_x - *mv_x + dynamic_search_range])
        {
          mv_cost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);    
          mcost   = simplified_add_up_SAD_quarter_pel(pic_pix_x, pic_pix_y, blocksize_x, blocksize_y,
                                  cand_mv_x + pic4_pix_x, cand_mv_y + pic4_pix_y,
                                  ref_picture, orig_pic, mv_cost, min_mcost, useABT, blocktype);
          simplified_SearchState[cand_mv_y - *mv_y + dynamic_search_range][cand_mv_x - *mv_x + 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_x = currmv_x;
            *mv_y = currmv_y;
            return min_mcost;
          }
        }
      }
    }
    // If the minimum cost point is in the center, break out the loop
    if (abort_search)
    {
      break;
    }
  }
  
  *mv_x = currmv_x;
  *mv_y = currmv_y;
  return min_mcost;
}

/*!
 ************************************************************************
 * \brief
 *    Set neighbouring block mode (intra/inter)
 *    used for fast motion estimation
 ************************************************************************
 */
void simplified_decide_intrabk_SAD()
{
  if (img->type != I_SLICE)
  {
    if (img->pix_x == 0 && img->pix_y == 0)
    {
      simplified_flag_intra_SAD = 0;
    }
    else if (img->pix_x == 0)
    {
      simplified_flag_intra_SAD = simplified_flag_intra[(img->pix_x)>>4];
    }
    else if (img->pix_y == 0)
    {
      simplified_flag_intra_SAD = simplified_flag_intra[((img->pix_x)>>4)-1];
    }
    else
    {
      simplified_flag_intra_SAD = ((simplified_flag_intra[(img->pix_x)>>4])||
                                   (simplified_flag_intra[((img->pix_x)>>4)-1])||
                                   (simplified_flag_intra[((img->pix_x)>>4)+1])) ;
    }
  }
  return;
}

/*!
 ************************************************************************
 * \brief
 *    Set cost to zero if neighbouring block is intra
 *    used for fast motion estimation
 ************************************************************************
 */
void simplified_skip_intrabk_SAD(int best_mode, int ref_max)
{
  short i, j, k;

  if (img->number > 0)
  {
    simplified_flag_intra[(img->pix_x)>>4] = (best_mode == 9 || best_mode == 10) ? 1 : 0;
  }

  if (img->type != I_SLICE  && (best_mode == 9 || best_mode == 10))
  {
    for (i=0; i < 4; i++)
    {
      for (j=0; j < 4; j++)
      {
        for (k=0; k < 9;k++)
        {
          simplified_fastme_l0_cost[k][j][i] = 0;
          simplified_fastme_l1_cost[k][j][i] = 0;
        }
      }
    }
  }
  return;
}

/*!
 ************************************************************************
 * \brief
 *    Set up prediction MV and prediction up layer cost
 *    used for fast motion estimation
 ************************************************************************
 */
void simplified_setup_FME(short ref,
                          int list,
                          int block_y,
                          int block_x,
                          int blocktype,
                          short ******all_mv)
{
  if (blocktype > 6)
  {
    simplified_pred_MV_uplayer_X = all_mv[block_y][block_x][list][ref][5][0];
    simplified_pred_MV_uplayer_Y = all_mv[block_y][block_x][list][ref][5][1];
  }
  else if (blocktype > 4)
  {
    simplified_pred_MV_uplayer_X = all_mv[block_y][block_x][list][ref][4][0];
    simplified_pred_MV_uplayer_Y = all_mv[block_y][block_x][list][ref][4][1];
  }
  else if (blocktype == 4)
  {
    simplified_pred_MV_uplayer_X = all_mv[block_y][block_x][list][ref][2][0];
    simplified_pred_MV_uplayer_Y = all_mv[block_y][block_x][list][ref][2][1];
  }
  else if (blocktype > 1)
  {
    simplified_pred_MV_uplayer_X = all_mv[block_y][block_x][list][ref][1][0];
    simplified_pred_MV_uplayer_Y = all_mv[block_y][block_x][list][ref][1][1];
  }

  if (blocktype > 1)
  {
    if (blocktype > 6)
    {   
      simplified_pred_SAD_uplayer = (list==1) ? 
          (simplified_fastme_l1_cost[5][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x])
          : (simplified_fastme_l0_cost[5][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x]);
      simplified_pred_SAD_uplayer /= 2; 	     
    }
    else if (blocktype > 4)
    {
      simplified_pred_SAD_uplayer = (list==1) ?
          (simplified_fastme_l1_cost[4][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x])
          : (simplified_fastme_l0_cost[4][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x]);
      simplified_pred_SAD_uplayer /= 2; 
    }
    else if (blocktype == 4)
    {
      simplified_pred_SAD_uplayer = (list==1) ?
          (simplified_fastme_l1_cost[2][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x])
          : (simplified_fastme_l0_cost[2][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x]);
      simplified_pred_SAD_uplayer /= 2; 
    }
    else
    {
      simplified_pred_SAD_uplayer = (list==1) ?
          (simplified_fastme_l1_cost[1][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x])
          : (simplified_fastme_l0_cost[1][(img->pix_y>>2)+block_y][(img->pix_x>>2)+block_x]);
      simplified_pred_SAD_uplayer /= 2;
    }

    simplified_pred_SAD_uplayer = simplified_flag_intra_SAD ? 0 : simplified_pred_SAD_uplayer;
  }
}

⌨️ 快捷键说明

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