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

📄 macroblock.c

📁 比较老的264解码器baseline实现
💻 C
📖 第 1 页 / 共 5 页
字号:
  int     i0      = (pic_pix_x << 2) + mv[0], i1=i0+pix_add, i2=i1+pix_add, i3=i2+pix_add;

  pel_t (*get_pel) (pel_t**, int, int) = UMVPelY_14;

  // Tian Dong: PLUS1, June 06, 2002
  incr      = (ref==-1 ? (!img->fld_type&&mref==mref_fld): direct_mode ? (!img->fld_type&&mref==mref_fld) : (mref==mref_fld)) ;
  
  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
    incr    = (ref==-1 ? (img->top_field&&img->field_mode): direct_mode ? (img->top_field&&img->field_mode):img->field_mode);

  ref_pic   = img->type==B_SLICE? mref [ref+1+incr] : mref [ref];

  *mpred++ = get_pel (ref_pic, j0, i0);
  *mpred++ = get_pel (ref_pic, j0, i1);
  *mpred++ = get_pel (ref_pic, j0, i2);
  *mpred++ = get_pel (ref_pic, j0, i3);
  *mpred++ = get_pel (ref_pic, j1, i0);
  *mpred++ = get_pel (ref_pic, j1, i1);
  *mpred++ = get_pel (ref_pic, j1, i2);
  *mpred++ = get_pel (ref_pic, j1, i3);
  *mpred++ = get_pel (ref_pic, j2, i0);
  *mpred++ = get_pel (ref_pic, j2, i1);
  *mpred++ = get_pel (ref_pic, j2, i2);
  *mpred++ = get_pel (ref_pic, j2, i3);
  *mpred++ = get_pel (ref_pic, j3, i0);
  *mpred++ = get_pel (ref_pic, j3, i1);
  *mpred++ = get_pel (ref_pic, j3, i2);
  *mpred++ = get_pel (ref_pic, j3, i3);
}


/*!
 ************************************************************************
 * \brief
 *    Predict one 4x4 Luma block
 ************************************************************************
 */
void
LumaPrediction4x4 (int  block_x,    // <--  relative horizontal block coordinate of 4x4 block
                   int  block_y,    // <--  relative vertical   block coordinate of 4x4 block
                   int  fw_mode,    // <--  forward  prediction mode (1-7, 0=DIRECT if bw_mode=0)
                   int  bw_mode,    // <--  backward prediction mode (1-7, 0=DIRECT if fw_mode=0)
                   int  fw_ref,      // <--  reference frame for forward prediction (-1: Intra4x4 pred. with fw_mode)
                                   int  bw_ref  )    
{
  static int fw_pred[16];
  static int bw_pred[16];

  int  i, j;
  int  block_x4  = block_x+4;
  int  block_y4  = block_y+4;
  int  pic_pix_x = img->pix_x + block_x;
  int  pic_pix_y = img->pix_y + block_y;
  int  bx        = block_x >> 2;
  int  by        = block_y >> 2;
  int* fpred     = fw_pred;
  int* bpred     = bw_pred;
  int  direct    = (fw_mode == 0 && bw_mode == 0 && (img->type == B_SLICE || img->type == BS_IMG));
  int  skipped   = (fw_mode == 0 && bw_mode == 0 && (img->type != B_SLICE && img->type != BS_IMG));
  int  *****fmv_array = img->all_mv;    // For MB level frame/field coding
  int  *****bmv_array = img->all_bmv;   // For MB level frame/field coding
  int apply_weights = ( (input->WeightedPrediction && (img->type == P_SLICE || img->type == SP_SLICE)) ||
                        (input->WeightedBiprediction && (img->type == B_SLICE || img->type == BS_IMG)));
  int fw_ref_idx, bw_ref_idx;

  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    if(img->top_field)
    {
      pic_pix_y = img->field_pix_y + block_y;
      fmv_array = img->all_mv_top;
      bmv_array = img->all_bmv_top;
    }
    else
    {
      pic_pix_y = img->field_pix_y + block_y;
      fmv_array = img->all_mv_bot;
      bmv_array = img->all_bmv_bot;
    }

  }

  if (input->direct_type && direct)    
  {
    fw_ref= fwdir_refFrArr[pic_pix_y>>2][pic_pix_x>>2];
    bw_ref= bwdir_refFrArr[pic_pix_y>>2][pic_pix_x>>2];
  }

  if (img->type == BS_IMG)
  {
    fw_ref_idx = fw_ref;
    bw_ref_idx = (bw_ref < 2) ? 1-bw_ref : bw_ref;
  }
  else
  {
    fw_ref_idx = fw_ref;
    bw_ref_idx = bw_ref;
  }

  direct_mode = direct && input->direct_type==0;


  if (fw_mode ||(direct && (!input->direct_type || fw_ref !=-1) )|| skipped)
  {
    OneComponentLumaPrediction4x4 (fw_pred, pic_pix_x, pic_pix_y, fmv_array [bx][by][fw_ref][fw_mode], fw_ref);   
                 
  }
  if (bw_mode || (direct && (!input->direct_type || bw_ref !=-1) ))
  {
    if (img->type == BS_IMG)
      OneComponentLumaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, bmv_array[bx][by][bw_ref][bw_mode],  bw_ref);
    else
      OneComponentLumaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, bmv_array[bx][by][     0][bw_mode],  -1);
  }

  if (apply_weights)
  {
    if (direct || (fw_mode && bw_mode))
    {
      if (input->direct_type && direct)
      {
        for   (j=block_y; j<block_y4; j++)
          for (i=block_x; i<block_x4; i++)  
            if (fw_ref ==-1)
              img->mpr[i][j] = clip1a(((wp_weight[1][bw_ref_idx][0] * *bpred++ + wp_luma_round) >> luma_log_weight_denom) + wp_offset[1][bw_ref_idx][0]);
            else if (bw_ref ==-1 )
              img->mpr[i][j] = clip1a(((wp_weight[0][fw_ref_idx][0] * *fpred++ + wp_luma_round) >> luma_log_weight_denom) + wp_offset[0][fw_ref_idx][0] );
            else 
              img->mpr[i][j] = clip1a(((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ + wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ + 2*wp_luma_round) >> (luma_log_weight_denom + 1)) + ((wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 1)>>1)); 
      }
      else
        for   (j=block_y; j<block_y4; j++)
          for (i=block_x; i<block_x4; i++)  
              img->mpr[i][j] = clip1a(((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ + wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ + 2*wp_luma_round) >> (luma_log_weight_denom + 1)) + ((wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 1)>>1)); 
    }
    else if (img->type == BS_IMG)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ + wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ + wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 2*wp_luma_round) >> (luma_log_weight_denom + 1));
    }
    else if (fw_mode || skipped)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a((wp_weight[0][fw_ref_idx][0] * *fpred++ + wp_offset[0][fw_ref_idx][0] + wp_luma_round) >> luma_log_weight_denom);
    }
    else
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a((wp_weight[1][bw_ref_idx][0] * *bpred++ + wp_offset[1][bw_ref_idx][0] + wp_luma_round) >> luma_log_weight_denom);
    }
  }
  else
  {
    if (direct || (fw_mode && bw_mode))
    {
      if (input->direct_type && direct)
      {
        for   (j=block_y; j<block_y4; j++)
          for (i=block_x; i<block_x4; i++)  
            if (fw_ref ==-1)
              img->mpr[i][j] = *bpred++;
            else if (bw_ref ==-1 )
              img->mpr[i][j] = *fpred++;
            else 
              img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2; 
      }
      else
        for   (j=block_y; j<block_y4; j++)
          for (i=block_x; i<block_x4; i++)  
            img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2; 
    }
    else if (img->type == BS_IMG)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2; 
    }
    else if (fw_mode || skipped)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *fpred++;
    }
    else
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *bpred++;
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Residual Coding of an 8x8 Luma block (not for intra)
 ************************************************************************
 */
int                                       //  ==> coefficient cost
LumaResidualCoding8x8 (int  *cbp,         //  --> cbp (updated according to processed 8x8 luminance block)
                       int  *cbp_blk,     //  --> block cbp (updated according to processed 8x8 luminance block)
                       int  block8x8,     // <--  block number of 8x8 block
                       int  fw_mode,      // <--  forward  prediction mode (1-7, 0=DIRECT)
                       int  bw_mode,      // <--  backward prediction mode (1-7, 0=DIRECT)
                       int  fw_refframe,  // <--  reference frame for forward prediction
                       int  bw_refframe   // <--  reference frame for backward prediction
                       )
{
  int    block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero, cbp_blk_mask;
  int    coeff_cost = 0;
  int    mb_y       = (block8x8 / 2) << 3;
  int    mb_x       = (block8x8 % 2) << 3;
  int    cbp_mask   = 1 << block8x8;
  int    bxx, byy;                   // indexing curr_blk
  int    scrFlag = 0;                // 0=noSCR, 1=strongSCR, 2=jmSCR
  byte** imgY_original = imgY_org;
  int  pix_y    = img->pix_y;

  int    skipped    = (fw_mode == 0 && bw_mode == 0 && (img->type != B_SLICE && img->type != BS_IMG));

  if (img->type==B_SLICE || img->type==BS_IMG)
    scrFlag = 1;
  
  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    pix_y     = img->field_pix_y;
    imgY_original = img->top_field ? imgY_org_top:imgY_org_bot;
  }


  //===== loop over 4x4 blocks =====
  for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
  {
    pic_pix_y = pix_y + block_y;

    for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
    {
      pic_pix_x = img->pix_x + block_x;

      cbp_blk_mask = (block_x>>2) + block_y;

      //===== prediction of 4x4 block =====
      LumaPrediction4x4 (block_x, block_y, fw_mode, bw_mode, fw_refframe, bw_refframe);

      //===== get displaced frame difference ======                
      for (j=0; j<4; j++)
      for (i=0; i<4; i++)
      {
        img->m7[i][j] = imgY_original[pic_pix_y+j][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
      }

      //===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====      
      if (img->NoResidueDirect != 1 && !skipped  )
      {
        //===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
        if (img->type!=SP_SLICE)  nonzero = dct_luma   (block_x, block_y, &coeff_cost, 0);
        else                      nonzero = dct_luma_sp(block_x, block_y, &coeff_cost);
        if (nonzero)
        {
          (*cbp_blk) |= 1 << cbp_blk_mask;  // one bit for every 4x4 block
          (*cbp)     |= cbp_mask;           // one bit for the 4x4 blocks of an 8x8 block
        }
      }
    }
  }

  /*
  The purpose of the action below is to prevent that single or 'expensive' coefficients are coded.
  With 4x4 transform there is larger chance that a single coefficient in a 8x8 or 16x16 block may be nonzero.
  A single small (level=1) coefficient in a 8x8 block will cost: 3 or more bits for the coefficient,
  4 bits for EOBs for the 4x4 blocks,possibly also more bits for CBP.  Hence the total 'cost' of that single
  coefficient will typically be 10-12 bits which in a RD consideration is too much to justify the distortion improvement.
  The action below is to watch such 'single' coefficients and set the reconstructed block equal to the prediction according
  to a given criterium.  The action is taken only for inter luma blocks.

  Notice that this is a pure encoder issue and hence does not have any implication on the standard.
  coeff_cost is a parameter set in dct_luma() and accumulated for each 8x8 block.  If level=1 for a coefficient,
  coeff_cost is increased by a number depending on RUN for that coefficient.The numbers are (see also dct_luma()): 3,2,2,1,1,1,0,0,...
  when RUN equals 0,1,2,3,4,5,6, etc.
  If level >1 coeff_cost is increased by 9 (or any number above 3). The threshold is set to 3. This means for example:
  1: If there is one coefficient with (RUN,level)=(0,1) in a 8x8 block this coefficient is discarded.
  2: If there are two coefficients with (RUN,level)=(1,1) and (4,1) the coefficients are also discarded
  sum_cnt_nonz is the accumulation of coeff_cost over a whole macro block.  If sum_cnt_nonz is 5 or less for the whole MB,
  all nonzero coefficients are discarded for the MB and the reconstructed block is set equal to the prediction.
  */

  if (img->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_)
  {
    coeff_cost  = 0;
    (*cbp)     &=  (63 - cbp_mask);
    (*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8%2)));

    for (i=mb_x; i<mb_x+8; i++)
    for (j=mb_y; j<mb_y+8; j++)
    {
      enc_picture->imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
    }
    if (img->type==SP_SLICE)
    {
      for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i+=BLOCK_SIZE)
        for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j+=BLOCK_SIZE)
          copyblock_sp(i,j);
    }
  }

  return coeff_cost;
}


/*!
 ************************************************************************
 * \brief
 *    Set mode parameters and reference frames for an 8x8 block
 ************************************************************************
 */
void
SetModesAndRefframe (int b8, int* fw_mode, int* bw_mode, int* fw_ref, int* bw_ref)
{
  Macroblock* currMB = &img->mb_data[img->current_mb_nr];
  int         j      = 2*(b8/2);
  int         i      = 2*(b8%2);
  int**     frefarr = refFrArr;   // For MB level field/frame coding
  int**     fw_refarr = fw_refFrArr;  // For MB level field/frame coding
  int**     bw_refarr = bw_refFrArr;  // For MB level field/frame coding
  int     block_y = img->block_y; // For MB level field/frame coding


  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    block_y = img->field_block_y;
    if(img->top_field)
    {
      frefarr = refFrArr_top;
      fw_refarr = fw_refFrArr_top;
      bw_refarr = bw_refFrArr_top;
    }
    else
    {
      frefarr = refFrArr_bot;
      fw_refarr = fw_refFrArr_bot;
      bw_refarr = bw_refFrArr_bot;
    }
  }

  *fw_mode = *bw_mode = *fw_ref = *bw_ref = -1;

  if (img->type!=B_SLICE && img->type!=BS_IMG)
  {
    *fw_ref = frefarr[block_y+j][img->block_x+i];
    *bw_ref = 0;
    *bw_mode  = 0;
    *fw_mode  = currMB->b8mode[b8];
  }
  else
  {
    if (currMB->b8pdir[b8]==-1)
    {
      *fw_ref   = -1;
      *bw_ref   = -1;
      *fw_mode  =  0;
      *bw_mode  =  0;
    }
    else if (currMB->b8pdir[b8]==0)
    {
      *fw_ref   = fw_refarr[block_y+j][img->block_x+i];
      *bw_ref   = 0;
      *fw_mode  = currMB->b8mode[b8];
      *bw_mode  = 0;
    }

⌨️ 快捷键说明

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