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

📄 me_distortion.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 4 页
字号:
}

/*!
************************************************************************
* \brief
*    SAD computation
************************************************************************
*/
int computeSAD(imgpel* src_pic,
               int blocksize_y,
               int blocksize_x,
               int min_mcost,
               int cand_x,
               int cand_y)
{
  int mcost = 0;
  int y,x;
  int pad_size_x = img_padded_size_x - blocksize_x;

  src_line = src_pic;
  ref_line = get_line[ref_access_method] (ref_pic_sub.luma, cand_y, cand_x);
  for (y=0; y<blocksize_y; y++)
  {
    for (x = 0; x < blocksize_x; x+=4)
    {
      mcost += byte_abs[ *src_line++ - *ref_line++ ];
      mcost += byte_abs[ *src_line++ - *ref_line++ ];
      mcost += byte_abs[ *src_line++ - *ref_line++ ];
      mcost += byte_abs[ *src_line++ - *ref_line++ ];
    }
    if (mcost >= min_mcost) return mcost;
    ref_line += pad_size_x;
  }
  if ( ChromaMEEnable ) 
  {
    // calculate chroma conribution to motion compensation error
    int blocksize_x_cr = blocksize_x >> shift_cr_x;
    int blocksize_y_cr = blocksize_y >> shift_cr_y;
    int cr_pad_size_x = img_cr_padded_size_x - blocksize_x_cr;
    int k;
    int mcr_cost = 0; // chroma me cost

    for (k=0; k < 2; k++)
    {
      src_line = src_pic + (256 << k);
      ref_line = get_crline[ref_access_method] ( ref_pic_sub.crcb[k], cand_y, cand_x);
      mcr_cost = 0;

      for (y=0; y<blocksize_y_cr; y++)
      {
        for (x = 0; x < blocksize_x_cr; x += 2)
        {
          mcr_cost += byte_abs[ *src_line++ - *ref_line++ ];
          mcr_cost += byte_abs[ *src_line++ - *ref_line++ ];
        }
        ref_line += cr_pad_size_x;
      }
      mcost += params->ChromaMEWeight * mcr_cost;
      if (mcost >= min_mcost) return mcost;
    }
  }

  return mcost;
}

/*!
************************************************************************
* \brief
*    SAD computation for weighted samples
************************************************************************
*/
int computeSADWP(imgpel* src_pic,
               int blocksize_y,
               int blocksize_x,
               int min_mcost,
               int cand_x,
               int cand_y)
{
  int mcost = 0;
  int y, x;
  int weighted_pel;
  int pad_size_x = img_padded_size_x - blocksize_x;

  src_line = src_pic;
  ref_line = get_line[ref_access_method] (ref_pic_sub.luma, cand_y, cand_x);
  for (y=0; y<blocksize_y; y++)
  {
    for (x = 0; x < blocksize_x; x+=4)
    {
      weighted_pel = iClip1( img->max_imgpel_value, ((weight_luma * *ref_line++  + wp_luma_round) >> luma_log_weight_denom) + offset_luma);
      mcost += byte_abs[ *src_line++ -  weighted_pel ];
      weighted_pel = iClip1( img->max_imgpel_value, ((weight_luma * *ref_line++  + wp_luma_round) >> luma_log_weight_denom) + offset_luma);
      mcost += byte_abs[ *src_line++ -  weighted_pel ];
      weighted_pel = iClip1( img->max_imgpel_value, ((weight_luma * *ref_line++  + wp_luma_round) >> luma_log_weight_denom) + offset_luma);
      mcost += byte_abs[ *src_line++ -  weighted_pel ];
      weighted_pel = iClip1( img->max_imgpel_value, ((weight_luma * *ref_line++  + wp_luma_round) >> luma_log_weight_denom) + offset_luma);
      mcost += byte_abs[ *src_line++ -  weighted_pel ];
    }
    if (mcost >= min_mcost) return mcost;
    ref_line += pad_size_x;
  }
  if ( ChromaMEEnable ) 
  {
    // calculate chroma conribution to motion compensation error
    int blocksize_x_cr = blocksize_x >> shift_cr_x;
    int blocksize_y_cr = blocksize_y >> shift_cr_y;
    int cr_pad_size_x = img_cr_padded_size_x - blocksize_x_cr;
    int k;
    int mcr_cost = 0;
    int max_imgpel_value_uv = img->max_imgpel_value_comp[1];

    for (k=0; k < 2; k++)
    {
      mcr_cost = 0;
      src_line = src_pic + (256 << k);
      ref_line = get_crline[ref_access_method] ( ref_pic_sub.crcb[k], cand_y, cand_x);
      for (y=0; y<blocksize_y_cr; y++)
      {
        for (x = 0; x < blocksize_x_cr; x+=2)
        {
          weighted_pel = iClip1( max_imgpel_value_uv, ((weight_cr[k] * *ref_line++  + wp_chroma_round) >> chroma_log_weight_denom) + offset_cr[k]);
          mcr_cost += byte_abs[ *src_line++ -  weighted_pel ];
          weighted_pel = iClip1( max_imgpel_value_uv, ((weight_cr[k] * *ref_line++  + wp_chroma_round) >> chroma_log_weight_denom) + offset_cr[k]);
          mcr_cost += byte_abs[ *src_line++ -  weighted_pel ];
        }
        ref_line += cr_pad_size_x;
      }
      mcost += params->ChromaMEWeight * mcr_cost;
      if (mcost >= min_mcost) return mcost;
    }
  }

  return mcost;
}

/*!
************************************************************************
* \brief
*    BiPred SAD computation (no weights)
************************************************************************
*/
int computeBiPredSAD1(imgpel* src_pic,
                      int blocksize_y,
                      int blocksize_x,
                      int min_mcost,
                      int cand_x1, int cand_y1,
                      int cand_x2, int cand_y2)
{
  int mcost = 0;
  int bi_diff;
  int y,x;
  int pad_size_x = img_padded_size_x - blocksize_x;

  src_line   = src_pic;
  ref2_line  = get_line[bipred2_access_method] (ref_pic2_sub.luma, cand_y2, cand_x2);
  ref1_line  = get_line[bipred1_access_method] (ref_pic1_sub.luma, cand_y1, cand_x1);

  for (y = 0; y < blocksize_y; y++)
  {
    for (x = 0; x < blocksize_x; x+=4)
    {
      bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
      mcost += byte_abs[bi_diff];
      bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
      mcost += byte_abs[bi_diff];
      bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
      mcost += byte_abs[bi_diff];
      bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
      mcost += byte_abs[bi_diff];
    }
    if (mcost >= min_mcost) return mcost;
    ref2_line += pad_size_x;
    ref1_line += pad_size_x;
  }

  if ( ChromaMEEnable ) 
  {
    // calculate chroma conribution to motion compensation error
    int blocksize_x_cr = blocksize_x >> shift_cr_x;
    int blocksize_y_cr = blocksize_y >> shift_cr_y;
    int cr_pad_size_x = img_cr_padded_size_x - blocksize_x_cr;
    int k;
    int mcr_cost = 0;

    for (k=0; k<2; k++)
    {
      mcr_cost = 0;
      src_line = src_pic + (256 << k);
      ref2_line = get_crline[bipred2_access_method] ( ref_pic2_sub.crcb[k], cand_y2, cand_x2);
      ref1_line = get_crline[bipred1_access_method] ( ref_pic1_sub.crcb[k], cand_y1, cand_x1);      

      for (y=0; y<blocksize_y_cr; y++)
      {
        for (x = 0; x < blocksize_x_cr; x+=2)
        {
          bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
          mcr_cost += byte_abs[bi_diff];
          bi_diff = (*src_line++) - ((*ref1_line++ + *ref2_line++ + 1)>>1);
          mcr_cost += byte_abs[bi_diff];
        }        
        ref2_line += cr_pad_size_x;
        ref1_line += cr_pad_size_x;
      }
      mcost += params->ChromaMEWeight * mcr_cost;
      if (mcost >= min_mcost) return mcost;
    }
  }
  return mcost;
}

/*!
************************************************************************
* \brief
*    BiPred SAD computation (with weights)
************************************************************************
*/
int computeBiPredSAD2(imgpel* src_pic,
                      int blocksize_y,
                      int blocksize_x,
                      int min_mcost,
                      int cand_x1, int cand_y1,
                      int cand_x2, int cand_y2)
{
  int mcost = 0;
  int bi_diff;
  int denom = luma_log_weight_denom + 1;
  int lround = 2 * wp_luma_round;
  int y,x;
  int weighted_pel, pixel1, pixel2;
  int pad_size_x = img_padded_size_x - blocksize_x;

  src_line   = src_pic;
  ref2_line  = get_line[bipred2_access_method] (ref_pic2_sub.luma, cand_y2, cand_x2);
  ref1_line  = get_line[bipred1_access_method] (ref_pic1_sub.luma, cand_y1, cand_x1);

  for (y=0; y<blocksize_y; y++)
  {
    for (x = 0; x < blocksize_x; x+=4)
    {
      pixel1 = weight1 * (*ref1_line++);
      pixel2 = weight2 * (*ref2_line++);
      weighted_pel =  iClip1( img->max_imgpel_value, ((pixel1 + pixel2 + lround) >> denom) + offsetBi);
      bi_diff = (*src_line++) - weighted_pel;
      mcost += byte_abs[bi_diff];

      pixel1 = weight1 * (*ref1_line++);
      pixel2 = weight2 * (*ref2_line++);
      weighted_pel =  iClip1( img->max_imgpel_value, ((pixel1 + pixel2 + lround) >> denom) + offsetBi);
      bi_diff = (*src_line++) - weighted_pel;
      mcost += byte_abs[bi_diff];

      pixel1 = weight1 * (*ref1_line++);
      pixel2 = weight2 * (*ref2_line++);
      weighted_pel =  iClip1( img->max_imgpel_value, ((pixel1 + pixel2 + lround) >> denom) + offsetBi);
      bi_diff = (*src_line++) - weighted_pel;
      mcost += byte_abs[bi_diff];

      pixel1 = weight1 * (*ref1_line++);
      pixel2 = weight2 * (*ref2_line++);
      weighted_pel =  iClip1( img->max_imgpel_value, ((pixel1 + pixel2 + lround) >> denom) + offsetBi);
      bi_diff = (*src_line++) - weighted_pel;
      mcost += byte_abs[bi_diff];
    }
    if (mcost >= min_mcost) return mcost;
    ref2_line += pad_size_x;
    ref1_line += pad_size_x;
  }

  if ( ChromaMEEnable ) 
  {
    // calculate chroma conribution to motion compensation error
    int blocksize_x_cr = blocksize_x >> shift_cr_x;
    int blocksize_y_cr = blocksize_y >> shift_cr_y;
    int cr_pad_size_x = img_cr_padded_size_x - blocksize_x_cr;
    int k;
    int mcr_cost = 0;
    int max_imgpel_value_uv = img->max_imgpel_value_comp[1];

    for (k=0; k<2; k++)
    {
      mcr_cost = 0;
      src_line = src_pic + (256 << k);
      ref2_line = get_crline[bipred2_access_method] ( ref_pic2_sub.crcb[k], cand_y2, cand_x2);
      ref1_line = get_crline[bipred1_access_method] ( ref_pic1_sub.crcb[k], cand_y1, cand_x1);

      for (y=0; y<blocksize_y_cr; y++)
      {
        for (x = 0; x < blocksize_x_cr; x+=2)
        {
          pixel1 = weight1_cr[k] * (*ref1_line++);
          pixel2 = weight2_cr[k] * (*ref2_line++);
          weighted_pel =  iClip1( max_imgpel_value_uv, ((pixel1 + pixel2 + lround) >> denom) + offsetBi_cr[k]);
          bi_diff = (*src_line++) - weighted_pel;
          mcr_cost += byte_abs[bi_diff];

          pixel1 = weight1_cr[k] * (*ref1_line++);
          pixel2 = weight2_cr[k] * (*ref2_line++);
          weighted_pel =  iClip1( max_imgpel_value_uv, ((pixel1 + pixel2 + lround) >> denom) + offsetBi_cr[k]);
          bi_diff = (*src_line++) - weighted_pel;
          mcr_cost += byte_abs[bi_diff];
        }
        ref2_line += cr_pad_size_x;
        ref1_line += cr_pad_size_x;
      }
      mcost += params->ChromaMEWeight * mcr_cost;
      if (mcost >= min_mcost) return mcost;
      
    }
  }
  return mcost;
}

/*!
 ************************************************************************
 * \brief
 *    SAD computation _with_ Hadamard Transform
 ************************************************************************
*/
int computeSATD(imgpel* src_pic,
                int blocksize_y,
                int blocksize_x,
                int min_mcost,
                int cand_x,
                int cand_y)
{
  int mcost = 0;
  int y, x, y4, *d;
  int src_size_x, src_size_mul;
  imgpel *src_tmp = src_pic;

  if ( !test8x8transform )
  { // 4x4 TRANSFORM
    src_size_x = blocksize_x - BLOCK_SIZE;
    src_size_mul = blocksize_x * BLOCK_SIZE;
    for (y = cand_y; y < cand_y + (blocksize_y<<2); y += (BLOCK_SIZE_SP))
    {
      for (x=0; x<blocksize_x; x += BLOCK_SIZE)
      {
        d    = diff;
        ref_line = get_line[ref_access_method] (ref_pic_sub.luma, y, cand_x + (x<<2));

⌨️ 快捷键说明

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