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

📄 macroblock.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:
}


/*!
 ************************************************************************
 * \brief
 *    Makes the decision if 8x8 tranform will be used (for RD-off)
 ************************************************************************
 */
int TransformDecision (Macroblock *currMB, int block_check, int *cost)
{
  int    block_y, block_x, i, j, k;
  int    mb_y, mb_x, block8x8;  
  int    list_mode[2];
  short  list_ref_idx[2], p_dir;
  int    num_blks;
  int    cost8x8=0, cost4x4=0;
  int    *diff_ptr;
  imgpel (*curr_mpr)[16] = img->mpr[0];

  if(block_check==-1)
  {
    block8x8 = 0;
    num_blks = 4;
  }
  else
  {
    block8x8 = block_check;
    num_blks = block_check + 1;
  }
  
  for (; block8x8<num_blks; block8x8++)
  {
    SetModesAndRefframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);

    mb_y = (block8x8 >> 1) << 3;
    mb_x = (block8x8 & 0x01) << 3;
        
    //===== prediction of 8x8 block =====
    LumaPrediction (currMB, mb_x, mb_y, 8, 8, p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1]);

    //===== loop over 4x4 blocks =====
    for (k=0, block_y=mb_y; block_y<mb_y+8; block_y+=4)
    {     
      for (block_x=mb_x; block_x<mb_x+8; block_x+=4)
      {
        //===== get displaced frame difference ======
        diff_ptr=&diff64[k];
        for (j=block_y; j<block_y + 4; j++)
        {
          for (i = block_x; i < block_x + 4; i++, k++)
            diff64[k] = pCurImg[img->opix_y+j][img->opix_x + i] - curr_mpr[j][i];
        }
        cost4x4 += distortion4x4 (diff_ptr);
      }
    }
    cost8x8 += distortion8x8 (diff64);
  }

  if(input->Transform8x8Mode==2) //always allow 8x8 transform
    return 1;
  else if(cost8x8 < cost4x4)
    return 1;
  else
  {
    *cost += (cost4x4 - cost8x8);
    return 0;
  }
}

/*!
 ************************************************************************
 * \brief
 *    Predict (on-the-fly) one component of a chroma 4x4 block
 ************************************************************************
 */
void OneComponentChromaPrediction4x4_regenerate (
                                 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
                                 int         list_idx,   //!< reference picture list
                                 short       ref,        //!< reference index
                                 int         blocktype,  //!< block type
                                 int         uv)         //!< chroma component
{
  int     i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
  short*  mvb;

  int     f1_x = 64/img->mb_cr_size_x;
  int     f2_x=f1_x-1;

  int     f1_y = 64/img->mb_cr_size_y;
  int     f2_y=f1_y-1;

  int     f3=f1_x*f1_y, f4=f3>>1;
  int     list_offset = img->mb_data[img->current_mb_nr].list_offset;
  int     max_y_cr = (int) (list_offset ? (img->height_cr >> 1) - 1 : img->height_cr - 1);
  int     max_x_cr = (int) (img->width_cr - 1);
  int     jjx, iix;
  int     mb_cr_y_div4 = img->mb_cr_size_y>>2;
  int     mb_cr_x_div4 = img->mb_cr_size_x>>2;
  int     jpos;

  StorablePicture **list = listX[list_idx + list_offset];

  imgpel** refimage = list[ref]->imgUV[uv];

  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][list_idx][ref][blocktype];

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

      if (active_sps->chroma_format_idc == 1)
        jj  += list[ref]->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
                                 int         list_idx,   //!< reference picture list
                                 short       ref,        //!< reference index
                                 int         blocktype,  //!< block type
                                 int         uv)         //!< chroma component
{
  int     j, ii, jj;
  short*  mvb;

  int     list_offset = img->mb_data[img->current_mb_nr].list_offset;

  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 + 2 + img->opix_c_x) << chroma_shift_x) + IMG_PAD_SIZE_TIMES4;


  StorablePicture **list = listX[list_idx + list_offset];

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

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

  width_pad_cr  = list[ref]->size_x_cr_pad;
  height_pad_cr = list[ref]->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][list_idx][ref][blocktype];

    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][list_idx][ref][blocktype];

    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
 ************************************************************************
 */
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
 ************************************************************************
 */
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 mode = currMB->c_ipred_mode;
  int j;
  imgpel (*curr_mpr)[16]           = img->mpr[ uv + 1];
  imgpel (*curr_mpr_16x16)[16][16] = img->mpr_16x16[uv + 1];

  //===== prediction =====
  for (j=block_y; j<block_y+4; j++)
    memcpy(&curr_mpr[j][block_x],&curr_mpr_16x16[mode][j][block_x], BLOCK_MULTIPLE * 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 
{
  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[by][bx];    
  int max_imgpel_value_uv = img->max_imgpel_value_comp[1];
  imgpel (*curr_mpr)[16] = img->mpr[ uv + 1];

  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 (currMB->bi_pred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && l0_mode==1 && l1_mode==1)
    mv_array = currMB->bi_pred_me == 1? img->bipred_mv1[by][bx] : img->bipred_mv2[by][bx];

  //===== INTRA PREDICTION =====
  if (p_dir==-1)
  {
    IntraChromaPrediction4x4 (currMB, uv, 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][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][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][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][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][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][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][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][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+1];
      int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][uv+1];
      int offset = (wp_offset[0][l0_ref_idx][uv+1] + wp_offset[1][l1_ref_idx][uv+1] + 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++)
          curr_mpr[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+1];
      int offset = wp_offset[0][l0_ref_idx][uv+1];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          curr_mpr[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+1];
      int offset = wp_offset[1][l1_ref_idx][uv+1];

      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          curr_mpr[j][i] = iClip1( max_imgpel_value_uv, ((wp * *l1pred++ + wp_chroma_round) >> chroma_log_weight_denom) + offset);
    }
  }
  else
  {
    if (p_dir==2)
    {
      for   (j=blo

⌨️ 快捷键说明

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