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

📄 mc_prediction.c

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

  for (j=block_c_y; j < block_c_y + BLOCK_SIZE; j++)
  {
    jjx = j/mb_cr_y_div4;
    jpos = (j + img->opix_c_y)*f1_y;

    for (i=block_c_x; i < block_c_x + BLOCK_SIZE; i++)
    {
      iix = i/mb_cr_x_div4;
      mvb  = mv [jjx][iix];

      ii   = (i + img->opix_c_x)*f1_x + mvb[0];
      jj   = jpos + mvb[1];

      if (active_sps->chroma_format_idc == 1)
        jj  += list->chroma_vector_adjustment;

      ii0  = iClip3 (0, max_x_cr, ii/f1_x);
      jj0  = iClip3 (0, max_y_cr, jj/f1_y);
      ii1  = iClip3 (0, max_x_cr, (ii+f2_x)/f1_x);
      jj1  = iClip3 (0, max_y_cr, (jj+f2_y)/f1_y);

      if1  = (ii&f2_x);  if0 = f1_x-if1;
      jf1  = (jj&f2_y);  jf0 = f1_y-jf1;

      *mpred++ = (if0 * jf0 * refimage[jj0][ii0] +
                  if1 * jf0 * refimage[jj0][ii1] +
                  if0 * jf1 * refimage[jj1][ii0] +
                  if1 * jf1 * refimage[jj1][ii1] + f4) / f3;
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Retrieve one component of a chroma 4x4 block from the buffer
 ************************************************************************
 */
void OneComponentChromaPrediction4x4_retrieve (imgpel*        mpred,      //!< array to store prediction values
                                 int         block_c_x,  //!< horizontal pixel coordinate of 4x4 block
                                 int         block_c_y,  //!< vertical   pixel coordinate of 4x4 block
                                 short***    mv,         //!< motion vector array
                                 StorablePicture *list,
                                 int         uv)         //!< chroma component
{
  int     j, ii, jj;
  short*  mvb;

  int     jjx;
  int     right_shift_x = 4 - chroma_shift_x;
  int     right_shift_y = 4 - chroma_shift_y;
  int     jpos;

  int     pos_x1 = block_c_x >> right_shift_x;
  int     pos_x2 = (block_c_x + 2) >> right_shift_x;
  int     ipos1 = ((block_c_x + img->opix_c_x    ) << chroma_shift_x) + IMG_PAD_SIZE_TIMES4;
  int     ipos2 = ((block_c_x + img->opix_c_x + 2) << chroma_shift_x) + IMG_PAD_SIZE_TIMES4;

  imgpel**** refsubimage = list->imgUV_sub[uv];
  imgpel *line_ptr;

  int jj_chroma = ((active_sps->chroma_format_idc == 1) ? list->chroma_vector_adjustment : 0) + IMG_PAD_SIZE_TIMES4;

  width_pad_cr  = list->size_x_cr_pad;
  height_pad_cr = list->size_y_cr_pad;

  for (j=block_c_y; j < block_c_y + BLOCK_SIZE; j++)
  {
    jjx = j >> right_shift_y; // translate into absolute block (luma) coordinates

    jpos = ( (j + img->opix_c_y) << chroma_shift_y ) + jj_chroma;

    mvb  = mv [jjx][pos_x1];

    ii   = ipos1 + mvb[0];
    jj   = jpos  + mvb[1];

    line_ptr = UMVLine8X_chroma ( refsubimage, jj, ii);
    *mpred++ = *line_ptr++;
    *mpred++ = *line_ptr;

    mvb  = mv [jjx][pos_x2];

    ii   = ipos2 + mvb[0];
    jj   = jpos  + mvb[1];

    line_ptr = UMVLine8X_chroma ( refsubimage, jj, ii);
    *mpred++ = *line_ptr++;
    *mpred++ = *line_ptr;
  }
}


/*!
 ************************************************************************
 * \brief
 *    Retrieve one component of a chroma block from the buffer
 ************************************************************************
 */
static inline 
void OneComponentChromaPrediction ( imgpel* mpred,      //!< array to store prediction values
                                   int    pic_pix_x,      //!< motion shifted horizontal coordinate of block
                                   int    pic_pix_y,      //!< motion shifted vertical  block
                                   int     block_size_x,   //!< horizontal block size
                                   int     block_size_y,   //!< vertical block size                                      
                                   StorablePicture *list, //!< reference picture list
                                   int         uv)         //!< chroma component
{
  int     j;
  imgpel *ref_line = UMVLine4X (list->imgUV_sub[uv], pic_pix_y, pic_pix_x);

  width_pad_cr  = list->size_x_cr_pad;
  height_pad_cr = list->size_y_cr_pad;

  for (j = 0; j < block_size_y; j++) 
  {
    memcpy(mpred, ref_line, block_size_x * sizeof(imgpel));
    ref_line += img_cr_padded_size_x;
    mpred += block_size_x;
  }
}

/*!
 ************************************************************************
 * \brief
 *    Predict an intra chroma 4x4 block
 ************************************************************************
 */
static inline
void IntraChromaPrediction4x4 (Macroblock* currMB, //! <-- Current Macroblock
                               int  uv,            //! <-- colour component
                               int  block_x,       //! <-- relative horizontal block coordinate of 4x4 block
                               int  block_y)       //! <-- relative vertical   block coordinate of 4x4 block
{
  int j;
  imgpel (*mb_pred)[16]       = img->mb_pred[ uv ];
  imgpel (*curr_mpr_16x16)[16] = img->mpr_16x16[uv][currMB->c_ipred_mode];

  //===== prediction =====
  for (j=block_y; j<block_y + BLOCK_SIZE; j++)
    memcpy(&mb_pred[j][block_x],&curr_mpr_16x16[j][block_x], BLOCK_SIZE * sizeof(imgpel));
}

/*!
 ************************************************************************
 * \brief
 *    Predict one chroma block
 ************************************************************************
 */
void ChromaPrediction ( Macroblock* currMB, // <-- Current Macroblock
                       int   uv,            // <-- colour component
                       int   block_x,       // <-- relative horizontal block coordinate of block
                       int   block_y,       // <-- relative vertical   block coordinate of block
                       int   block_size_x,  // <-- relative horizontal block coordinate of block
                       int   block_size_y,  // <-- relative vertical   block coordinate of block                        
                       int   p_dir,         // <-- prediction direction (0=list0, 1=list1, 2=bipred)
                       int   l0_mode,       // <-- list0  prediction mode (1-7, 0=DIRECT if l1_mode=0)
                       int   l1_mode,       // <-- list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                       short l0_ref_idx,    // <-- reference frame for list0 prediction (if (<0) -> intra prediction)
                       short l1_ref_idx,    // <-- reference frame for list1 prediction 
                       short bipred_me      // <-- use bi prediction mv (0=no bipred, 1 = use set 1, 2 = use set 2)
                       )    
{
  int  i, j;
  int  block_x4     = block_x + block_size_x;
  int  block_y4     = block_y + block_size_y;
  int  pic_opix_x   = ((img->opix_c_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
  int  pic_opix_y   = ((img->opix_c_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
  int  bx           = block_x >> 2;
  int  by           = block_y >> 2;
  imgpel* l0pred     = l0_pred;
  imgpel* l1pred     = l1_pred;
  short****** mv_array = img->all_mv;    
  int max_imgpel_value_uv = img->max_imgpel_value_comp[1];
  int uv_comp = uv + 1;
  imgpel (*mb_pred)[16] = img->mb_pred[ uv_comp];

  int  apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
    (active_pps->weighted_bipred_idc && (img->type == B_SLICE)));

  if (bipred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && is_bipred_enabled(l0_mode)  && is_bipred_enabled(l1_mode))
    mv_array = img->bipred_mv[bipred_me - 1]; 

  //===== INTRA PREDICTION =====
  if (p_dir==-1)
  {
    IntraChromaPrediction4x4 (currMB, uv_comp, block_x, block_y);
    return;
  }

  //===== INTER PREDICTION =====
  switch (p_dir)
  {
  case 0:
    OneComponentChromaPrediction (l0_pred, pic_opix_x + mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx][1], block_size_x, block_size_y, listX[0+currMB->list_offset][l0_ref_idx], uv);
    break;
  case 1: 
    OneComponentChromaPrediction (l1_pred, pic_opix_x + mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx][1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx], uv);
    break;
  case 2:
    OneComponentChromaPrediction (l0_pred, pic_opix_x + mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx][1], block_size_x, block_size_y, listX[0+currMB->list_offset][l0_ref_idx], uv);
    OneComponentChromaPrediction (l1_pred, pic_opix_x + mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx][1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx], uv);
    break;
  default:
    break;
  }

  if (apply_weights)
  {
    if (p_dir==2)
    {
      int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][uv_comp];
      int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][uv_comp];
      int offset = (wp_offset[0][l0_ref_idx][uv_comp] + wp_offset[1][l1_ref_idx][uv_comp] + 1)>>1;
      int wp_round = 2*wp_chroma_round;
      int weight_denom = luma_log_weight_denom + 1;


      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] =  iClip1( max_imgpel_value_uv,
          ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + (offset) );
    }
    else if (p_dir==0)
    {
      int wp = wp_weight[0][l0_ref_idx][uv_comp];
      int offset = wp_offset[0][l0_ref_idx][uv_comp];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = iClip1( max_imgpel_value_uv, (( wp * *l0pred++ + wp_chroma_round) >> chroma_log_weight_denom) +  offset);
    }
    else // (p_dir==1)
    {
      int wp = wp_weight[1][l1_ref_idx][uv_comp];
      int offset = wp_offset[1][l1_ref_idx][uv_comp];

      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = iClip1( max_imgpel_value_uv, ((wp * *l1pred++ + wp_chroma_round) >> chroma_log_weight_denom) + offset);
    }
  }
  else
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
    }
    else if (p_dir==0)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(mb_pred[j][block_x]), l0pred, block_size_x * sizeof(imgpel));
        l0pred += block_size_x;
      }
    }
    else // (p_dir==1)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(mb_pred[j][block_x]), l1pred, block_size_x * sizeof(imgpel));
        l1pred += block_size_x;
      }
    }
  }
}



/*!
 ************************************************************************
 * \brief
 *    Predict one chroma 4x4 block
 ************************************************************************
 */
void ChromaPrediction4x4 ( Macroblock* currMB, // <-- Current Macroblock
                           int   uv,           // <-- colour component
                           int   block_x,      // <-- relative horizontal block coordinate of 4x4 block
                           int   block_y,      // <-- relative vertical   block coordinate of 4x4 block
                           int   p_dir,        // <-- prediction direction (0=list0, 1=list1, 2=bipred)
                           int   l0_mode,      // <-- list0  prediction mode (1-7, 0=DIRECT if l1_mode=0)
                           int   l1_mode,      // <-- list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                           short l0_ref_idx,   // <-- reference frame for list0 prediction (if (<0) -> intra prediction)
                           short l1_ref_idx,   // <-- reference frame for list1 prediction 
                           short bipred_me     // <-- use bi prediction mv (0=no bipred, 1 = use set 1, 2 = use set 2)
                           )   
{
  int  i, j;
  int  block_x4  = block_x + BLOCK_SIZE;
  int  block_y4  = block_y + BLOCK_SIZE;
  imgpel* l0pred     = l0_pred;
  imgpel* l1pred     = l1_pred;
  short****** mv_array = img->all_mv;
  int max_imgpel_value_uv = img->max_imgpel_value_comp[1];
  int uv_comp = uv + 1;
  imgpel (*mb_pred)[16] = img->mb_pred[uv_comp];
  int     list_offset = currMB->list_offset;
  
  int  apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
    (active_pps->weighted_bipred_idc && (img->type == B_SLICE)));

  if (bipred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && is_bipred_enabled(l0_mode)  && is_bipred_enabled(l1_mode) )
    mv_array = img->bipred_mv[bipred_me - 1]; 
  //===== INTRA PREDICTION =====
  
  if (p_dir==-1)
  {
    IntraChromaPrediction4x4 (currMB, uv_comp, block_x, block_y);
    return;
  }

  //===== INTER PREDICTION =====
  switch (p_dir)
  {
  case 0: // LIST_0
    (*OneComponentChromaPrediction4x4) (l0_pred, block_x, block_y, mv_array[LIST_0][l0_ref_idx][l0_mode], listX[LIST_0 + list_offset][l0_ref_idx], uv);
    break;
  case 1: // LIST_1
    (*OneComponentChromaPrediction4x4) (l1_pred, block_x, block_y, mv_array[LIST_1][l1_ref_idx][l1_mode], listX[LIST_1 + list_offset][l1_ref_idx], uv);
    break;
  case 2: // BI_PRED
    (*OneComponentChromaPrediction4x4) (l0_pred, block_x, block_y, mv_array[LIST_0][l0_ref_idx][l0_mode], listX[LIST_0 + list_offset][l0_ref_idx], uv);
    (*OneComponentChromaPrediction4x4) (l1_pred, block_x, block_y, mv_array[LIST_1][l1_ref_idx][l1_mode], listX[LIST_1 + list_offset][l1_ref_idx], uv);
    break;
  default:
    break;
  }

  if (apply_weights)
  {
    if (p_dir==2)
    {
      int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][uv_comp];
      int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][uv_comp];
      int offset = (wp_offset[0][l0_ref_idx][uv_comp] + wp_offset[1][l1_ref_idx][uv_comp] + 1)>>1;
      int wp_round = 2 * wp_chroma_round;
      int weight_denom = luma_log_weight_denom + 1;

      for (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] =  iClip1( max_imgpel_value_uv,
          ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + (offset) );

⌨️ 快捷键说明

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