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

📄 macroblock.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 5 页
字号:
  else //! CABAC
  {
    for (i=0; i<currSlice->max_part_nr; i++)
    {
      dataPart= &(currSlice->partArr[i]);
      eep = &(dataPart->ee_cabac);

      if( arienco_bits_written(eep) > (params->slice_argument*8))
        return TRUE;
    }
  }
  
  return FALSE;
}


/*!
 ************************************************************************
 * \brief
 *    Residual Coding of an 8x8 Luma block (not for intra)
 *
 * \return
 *    coefficient cost
 ************************************************************************
 */
int LumaResidualCoding8x8 ( Macroblock* currMB, //!< Current Macroblock to be coded
                            int   *cbp,         //!< Output: cbp (updated according to processed 8x8 luminance block)
                            int64 *cbp_blk,     //!< Output: block cbp (updated according to processed 8x8 luminance block)
                            int   block8x8,     //!< block number of 8x8 block
                            short p_dir,        //!< prediction direction
                            int   l0_mode,      //!< list0 prediction mode (1-7, 0=DIRECT)
                            int   l1_mode,      //!< list1 prediction mode (1-7, 0=DIRECT)
                            short l0_ref_idx,   //!< reference picture for list0 prediction
                            short l1_ref_idx,   //!< reference picture for list0 prediction
                            int   is_cavlc      //!< entropy mode?
                           )
{
  int    block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero = 0, cbp_blk_mask;
  int    coeff_cost = 0;
  int    mb_y       = (block8x8 >> 1) << 3;
  int    mb_x       = (block8x8 & 0x01) << 3;
  int    cbp_mask   = 1 << block8x8;
  int    bxx, byy;                   // indexing curr_blk
  int    skipped    = (l0_mode == 0 && l1_mode == 0 && (img->type != B_SLICE));
   
  //set transform size
  int    need_8x8_transform = currMB->luma_transform_size_8x8_flag;
  int    uv, nonzerocr[3]={0,0,0};  
  short bipred_me = currMB->bipred_me[block8x8];
  coeff_cost_cr[1] = coeff_cost_cr[2] = 0;

  //===== loop over 4x4 blocks =====
  if(!need_8x8_transform)
  {
    if (((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5))
    {
      LumaPrediction (currMB, mb_x, mb_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me); 

      //===== compute prediction residual ======            
      ComputeResidue (pCurImg, img->mb_pred[0], img->mb_ores[0], mb_y, mb_x, img->opix_y, img->opix_x + mb_x, 8, 8);
    }

    for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
    {
      for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
      {
        pic_pix_x = img->opix_x + block_x;
        cbp_blk_mask = (block_x >> 2) + block_y;

        //===== prediction of 4x4 block =====
        if (!(((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5)))
        {
          LumaPrediction (currMB, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);

          //===== compute prediction residual ======            
          ComputeResidue(pCurImg, img->mb_pred[0], img->mb_ores[0], block_y, block_x, img->opix_y, pic_pix_x, 4, 4);
        }

        if (img->P444_joined)
        {
          for (uv = PLANE_U; uv <= PLANE_V; uv++)
          {
            select_plane((ColorPlane) (uv));
            ChromaPrediction (currMB, uv - 1, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
            
            //===== compute prediction residual ======            
            ComputeResidue(pImgOrg[uv], img->mb_pred[uv], img->mb_ores[uv], block_y, block_x, img->opix_y, pic_pix_x, 4, 4);
          }
          select_plane(PLANE_Y);
        }

        //===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
        if ( (img->NoResidueDirect != 1 && !skipped  ) ||
          ((currMB->qp_scaled[0])==0 && img->lossless_qpprime_flag==1) )
        {
          //===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
          nonzero = pDCT_4x4 (currMB, PLANE_Y, block_x, block_y, &coeff_cost, 0, is_cavlc);

          if (nonzero)
          {
            (*cbp_blk) |= (int64)1 << cbp_blk_mask;  // one bit for every 4x4 block
            (*cbp)     |= cbp_mask;           // one bit for the 4x4 blocks of an 8x8 block
          }

          if (img->P444_joined)
          {
            if (img->type!=SP_SLICE)  
            {
              for (uv = PLANE_U; uv <= PLANE_V; uv++)
              {
                select_plane((ColorPlane) uv);
                nonzerocr[uv] = pDCT_4x4( currMB, (ColorPlane) uv, block_x, block_y, &coeff_cost_cr[uv], 0, is_cavlc);
                if (nonzerocr[uv])
                {
                  (cur_cbp_blk[uv]) |= (int64) 1 << cbp_blk_mask;  // one bit for every 4x4 block
                  (cmp_cbp[uv]) |= cbp_mask;           // one bit for the 4x4 blocks of an 8x8 block
                }
              }
              select_plane(PLANE_Y);
            }
            else
            {
              assert(img->type==SP_SLICE);   //SP_SLICE not implementd for FREXT_AD444
            }
          }
        }
      }
    }
  }
  else
  {
    for (block_y = mb_y; block_y < mb_y + 8; block_y += 8)
    {
      pic_pix_y = img->opix_y + block_y;

      for (block_x = mb_x; block_x < mb_x + 8; block_x += 8)
      {
        pic_pix_x = img->opix_x + block_x;

        cbp_blk_mask = (block_x>>2) + block_y;

        //===== prediction of 4x4 block =====
        LumaPrediction (currMB, block_x, block_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);

        //===== compute prediction residual ======            
        ComputeResidue (pCurImg, img->mb_pred[0], img->mb_ores[0], block_y, block_x, img->opix_y, pic_pix_x, 8, 8);

        if (img->P444_joined) 
        {
          for (uv = PLANE_U; uv <= PLANE_V; uv++)
          {
            select_plane((ColorPlane) uv);
            ChromaPrediction (currMB, uv - 1, block_x, block_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);

            //===== compute prediction residual ======            
            ComputeResidue (pImgOrg[uv], img->mb_pred[uv], img->mb_ores[uv], block_y, block_x, img->opix_y, pic_pix_x, 8, 8);
          }
          select_plane(PLANE_Y);
        }
      }
    }

    if (img->NoResidueDirect != 1 && !skipped)
    {
      if (img->type!=SP_SLICE)
        nonzero = pDCT_8x8 (currMB, PLANE_Y, block8x8, &coeff_cost, 0);

      if (nonzero)
      {
        (*cbp_blk) |= 51 << (4*block8x8 - 2*(block8x8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
        (*cbp)     |= cbp_mask;                               // one bit for the 4x4 blocks of an 8x8 block
      }
      
      if(img->P444_joined) 
      {
        if (img->type!=SP_SLICE)
        {
          for (uv = PLANE_U; uv <= PLANE_V; uv++)
          {
            select_plane((ColorPlane) uv);
            nonzerocr[uv] = pDCT_8x8( currMB, (ColorPlane) uv, block8x8, &coeff_cost_cr[uv], 0);
            if (nonzerocr[uv])
            {
              (cur_cbp_blk[uv]) |= 51 << (4*block8x8-2*(block8x8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
              (cmp_cbp[uv])     |= cbp_mask;           // one bit for the 4x4 blocks of an 8x8 block
            }
          }
          select_plane(PLANE_Y);
        }        
      }
    }
  }

  /*
  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_4x4() 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_4x4()): 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[0] is the accumulation of coeff_cost over a whole macro block.  If sum_cnt_nonz[0] 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_ &&
    ((currMB->qp_scaled[0])!=0 || img->lossless_qpprime_flag==0)&&
    !(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1 )))// last set of conditions
    // cannot skip when perfect reconstruction is as in switching pictures or SI pictures
  {
    coeff_cost  = 0;
    (*cbp)     &=  (63 - cbp_mask);
    (*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8 & 0x01)));
    
    memset( img->cofAC[block8x8][0][0], 0, 4 * 2 * 65 * sizeof(int));
    
    for (j=mb_y; j<mb_y+8; j++)
      memcpy(&enc_picture->imgY[img->pix_y + j][img->pix_x + mb_x], &img->mb_pred[0][j][mb_x], 2 * BLOCK_SIZE * sizeof(imgpel));

    if (img->type==SP_SLICE)
    {
      for (i=mb_x; i < mb_x + BLOCK_SIZE_8x8; i+=BLOCK_SIZE)
        for (j=mb_y; j < mb_y + BLOCK_SIZE_8x8; j+=BLOCK_SIZE)
          copyblock_sp(currMB, PLANE_Y, i, j);
    }
  }

  if(img->P444_joined)
  {
    for (uv = PLANE_U; uv <= PLANE_V; uv++)
    {
      if (img->NoResidueDirect != 1 && !skipped && coeff_cost_cr[uv] <= _LUMA_COEFF_COST_ &&
        (currMB->qp_scaled[uv]!=0 || img->lossless_qpprime_flag==0))// last set of conditions
      {
        coeff_cost_cr[uv] = 0;
        cmp_cbp[uv] &= (63 - cbp_mask);
        cur_cbp_blk[uv] &= ~(51 << (4*block8x8-2*(block8x8 & 0x01)));

        memset( img->cofAC[block8x8 + 4 * uv][0][0], 0, 4 * 2 * 65 * sizeof(int));

        for (j=mb_y; j<mb_y+8; j++)
          memcpy(&enc_picture->imgUV[uv - 1][img->pix_y + j][img->pix_x + mb_x], &img->mb_pred[uv][j][mb_x], 2 * BLOCK_SIZE * sizeof(imgpel));
      }
    }
  }

  return coeff_cost;
}

/*!
 ************************************************************************
 * \brief
 *    Set mode parameters and reference frames for an 8x8 block
 ************************************************************************
 */
void SetModesAndRefframe (Macroblock* currMB, int b8, short* p_dir, int list_mode[2], short list_ref_idx[2])
{
  int j = 2*(b8>>1);
  int i = 2*(b8 & 0x01);

  list_mode[0] = list_mode[1] = list_ref_idx[0] = list_ref_idx[1] = -1;

  *p_dir  = currMB->b8pdir[b8];

  if (img->type!=B_SLICE)
  {
    list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
    list_ref_idx[1] = 0;
    list_mode[0] = currMB->b8mode[b8];
    list_mode[1] = 0;
  }
  else
  {
    if (currMB->b8pdir[b8]==-1)
    {
      list_ref_idx[0] = -1;
      list_ref_idx[1] = -1;
      list_mode[0] =  0;
      list_mode[1] =  0;
    }
    else if (currMB->b8pdir[b8]==0)
    {
      list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
      list_ref_idx[1] = 0;
      list_mode[0] = currMB->b8mode[b8];
      list_mode[1] = 0;
    }
    else if (currMB->b8pdir[b8]==1)
    {
      list_ref_idx[0] = 0;
      list_ref_idx[1] = enc_picture->motion.ref_idx[LIST_1][img->block_y+j][img->block_x+i];
      list_mode[0] = 0;
      list_mode[1] = currMB->b8mode[b8];
    }
    else
    {
      list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
      list_ref_idx[1] = enc_picture->motion.ref_idx[LIST_1][img->block_y+j][img->block_x+i];
      list_mode[0] = currMB->b8mode[b8];
      list_mode[1] = currMB->b8mode[b8];
    }
  }
}


/*!
 ************************************************************************
 * \brief
 *    Residual Coding of a Luma macroblock (not for intra)
 ************************************************************************
 */
void LumaResidualCoding (Macroblock *currMB, int is_cavlc)
{
  int uv, i,j,block8x8,b8_x,b8_y;
  int list_mode[2];
  short list_ref_idx[2];
  short p_dir;
  int sum_cnt_nonz[3] = {0 ,0, 0};
  //imgpel (*mb_pred)[16] = img->mb_pred[0];

  currMB->cbp     = 0;
  currMB->cbp_blk = 0;
  cmp_cbp[1] = cmp_cbp[2] = 0;
  cur_cbp_blk[1] = cur_cbp_blk[2] = 0;

  for (block8x8=0; block8x8<4; block8x8++)
  {    
    SetModesAndRefframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
    
    sum_cnt_nonz[0] += LumaResidualCoding8x8 (currMB, &(currMB->cbp), &(currMB->cbp_blk), block8x8,
      p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1], is_cavlc);
    
    if(img->P444_joined) 
    {
      sum_cnt_nonz[1] += coeff_cost_cr[1];
      sum_cnt_nonz[2] += coeff_cost_cr[2];
    }
  }

  if (sum_cnt_nonz[0] <= _LUMA_MB_COEFF_COST_ &&
    ((currMB->qp_scaled[0])!=0 || img->lossless_qpprime_flag==0) &&
    !(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1)))// modif ES added last set of conditions
    //cannot skip if SI or switching SP frame perfect reconstruction is needed
  {
    currMB->cbp     &= 0xfffff0 ;
    currMB->cbp_blk &= 0xff0000 ;
    for (j=0; j < MB_BLOCK_SIZE; j++)
      memcpy(&enc_picture->imgY[img->pix_y+j][img->pix_x], img->mb_pred[0][j], MB_BLOCK_SIZE * sizeof (imgpel));
        
    memset( img->cofAC[0][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));

    if (img->type==SP_SLICE)
    {
      for(block8x8=0;block8x8<4;block8x8++)
      {
        b8_x=(block8x8&1)<<3;
        b8_y=(block8x8&2)<<2;
        for (i = b8_x; i < b8_x + BLOCK_SIZE_8x8; i += 4)
          for (j = b8_y; j < b8_y + BLOCK_SIZE_8x8;j += 4)
            copyblock_sp(currMB, PLANE_Y, i, j);
      }
    }
  }

  if (img->P444_joined)
  {
    for (uv = PLANE_U; uv <= PLANE_V; uv++)
    {
      if(sum_cnt_nonz[uv] <= _LUMA_MB_COEFF_COST_ &&
        ((currMB->qp_scaled[uv])!=0 ||img->lossless_qpprime_flag==0)) 
      {
        cmp_cbp[uv] &= 0xfffff0 ;
        cur_cbp_blk[uv] &= 0xff0000 ;
        for (j=0; j < MB_BLOCK_SIZE; j++)
          memcpy(&enc_picture->p_img[uv][img->pix_y+j][img->pix_x], img->mb_pred[uv][j], MB_BLOCK_SIZE * sizeof (imgpel));

        memset( img->cofAC[4 * uv][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));
      }
      currMB->cbp |= cmp_cbp[uv];
    }
  }
}


/*!
 ************************************************************************
 * \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;

⌨️ 快捷键说明

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