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

📄 macroblock.c

📁 h.264/avc 视频编码程序,实现分数像素匹配功能,非原创.
💻 C
📖 第 1 页 / 共 5 页
字号:
  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 (coeff_cost <= 3)
  {
    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++)
    {
      imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
    }
    if (img->types==SP_IMG)
    {
      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* refframe)
{
  Macroblock* currMB = &img->mb_data[img->current_mb_nr];
  int         j      = 2*(b8/2);
  int         i      = 2*(b8%2);

  if (img->type!=B_IMG)
  {
    *refframe = refFrArr[img->block_y+j][img->block_x+i];
    *bw_mode  = 0;
    *fw_mode  = currMB->b8mode[b8];
  }
  else
  {
    if (currMB->b8pdir[b8]==-1)
    {
      *refframe = -1;
      *fw_mode  =  0;
      *bw_mode  =  0;
    }
    else if (currMB->b8pdir[b8]==0)
    {
      *refframe = fw_refFrArr[img->block_y+j][img->block_x+i];
      *fw_mode  = currMB->b8mode[b8];
      *bw_mode  = 0;
    }
    else if (currMB->b8pdir[b8]==1)
    {
      *refframe = 0;
      *fw_mode  = 0;
      *bw_mode  = currMB->b8mode[b8];
    }
    else
    {
      *refframe = fw_refFrArr[img->block_y+j][img->block_x+i];
      *fw_mode  = currMB->b8mode[b8];
      *bw_mode  = currMB->b8mode[b8];
      if (currMB->b8mode[b8]==0) // direct
      {
        *refframe = max(0,refFrArr[img->block_y+j][img->block_x+i]);
      }
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Residual Coding of a Luma macroblock (not for intra)
 ************************************************************************
 */
void
LumaResidualCoding ()
{
  int i,j,block8x8;
  int fw_mode, bw_mode, refframe;
  int sum_cnt_nonz;
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];

  currMB->cbp     = 0 ;
  currMB->cbp_blk = 0 ;
  sum_cnt_nonz    = 0 ;

  for (block8x8=0; block8x8<4; block8x8++)
  {
    SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe);

    sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,
                                           fw_mode, bw_mode, refframe);
  }
  
  if (sum_cnt_nonz <= 5 )
  {
     currMB->cbp     &= 0xfffff0 ;
     currMB->cbp_blk &= 0xff0000 ;
     for (i=0; i < MB_BLOCK_SIZE; i++)
     {
       for (j=0; j < MB_BLOCK_SIZE; j++)
       {
         imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
       }
     }
     if (img->types==SP_IMG)
     {
       for (i=0; i < MB_BLOCK_SIZE; i+=BLOCK_SIZE)
       for (j=0; j < MB_BLOCK_SIZE; j+=BLOCK_SIZE)
       {
         copyblock_sp(i,j);
       }
     }
   }
}



/*!
 ************************************************************************
 * \brief
 *    Predict one component of a chroma 4x4 block
 ************************************************************************
 */
void
OneComponentChromaPrediction4x4 (int*     mpred,      //  --> array to store prediction values
                                 int      pix_c_x,    // <--  horizontal pixel coordinate of 4x4 block
                                 int      pix_c_y,    // <--  vertical   pixel coordinate of 4x4 block
                                 int***** mv,         // <--  motion vector array
                                 int      ref,        // <--  reference frame parameter (0.../ -1: backward)
                                 int      blocktype,  // <--  block type
                                 int      uv)         // <--  chroma component
{
  int     i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
  int*    mvb;
  int     refframe  = (ref<0 ?      0 :      ref);
  pel_t** refimage  = (ref<0 ? mcef_P : mcef[ref])[uv];
  int     je        = pix_c_y + 4;
  int     ie        = pix_c_x + 4;
  int     f1        =(input->mv_res?16:8), f2=f1-1, f3=f1*f1, f4=f3>>1;
  int     s1        =(input->mv_res? 4:3);


  for (j=pix_c_y; j<je; j++)
  for (i=pix_c_x; i<ie; i++)
  {
    mvb  = mv [(i-img->pix_c_x)>>1][(j-img->pix_c_y)>>1][refframe][blocktype];
    ii   = (i<<s1) + mvb[0];
    jj   = (j<<s1) + mvb[1];

    ii0  = max (0, min (img->width_cr -1, ii>>s1     ));
    jj0  = max (0, min (img->height_cr-1, jj>>s1     ));
    ii1  = max (0, min (img->width_cr -1, (ii+f2)>>s1));
    jj1  = max (0, min (img->height_cr-1, (jj+f2)>>s1));

    if1  = (ii&f2);  if0 = f1-if1;
    jf1  = (jj&f2);  jf0 = f1-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
 *    Predict an intra chroma 4x4 block
 ************************************************************************
 */
void
IntraChromaPrediction4x4 (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     s=128, s0=0, s1=0, s2=0, s3=0, i, j;
  pel_t** image             = imgUV[uv];
  int     img_cx            = img->pix_c_x;
  int     img_cy            = img->pix_c_y;
  int     img_cx_1          = img->pix_c_x-1;
  int     img_cx_4          = img->pix_c_x+4;
  int     img_cy_1          = img->pix_c_y-1;
  int     img_cy_4          = img->pix_c_y+4;
  int     mb_nr             = img->current_mb_nr;
  int     mb_width          = img->width/16;
  int     mb_available_up   = (img_cy/BLOCK_SIZE == 0) ? 0 : (img->mb_data[mb_nr].slice_nr==img->mb_data[mb_nr-mb_width].slice_nr);
  int     mb_available_left = (img_cx/BLOCK_SIZE == 0) ? 0 : (img->mb_data[mb_nr].slice_nr==img->mb_data[mb_nr-1]       .slice_nr);

  if(input->UseConstrainedIntraPred)
  {
    if (mb_available_up   && (img->intra_block[mb_nr-mb_width][2]==0 || img->intra_block[mb_nr-mb_width][3]==0))
      mb_available_up   = 0;
    if (mb_available_left && (img->intra_block[mb_nr-       1][1]==0 || img->intra_block[mb_nr       -1][3]==0))
      mb_available_left = 0;
  }

  //===== get prediction value =====
  switch ((block_y>>1) + (block_x>>2))
  {
  case 0:  //===== TOP LEFT =====
    if      (mb_available_up)    for (i=0;i<4;i++)  s0 += image[img_cy_1  ][img_cx  +i];
    if      (mb_available_left)  for (i=0;i<4;i++)  s2 += image[img_cy  +i][img_cx_1  ];
    if      (mb_available_up && mb_available_left)  s  = (s0+s2+4) >> 3;
    else if (mb_available_up)                       s  = (s0   +2) >> 2;
    else if (mb_available_left)                     s  = (s2   +2) >> 2;
    break;
  case 1: //===== TOP RIGHT =====
    if      (mb_available_up)    for (i=0;i<4;i++)  s1 += image[img_cy_1  ][img_cx_4+i];
    else if (mb_available_left)  for (i=0;i<4;i++)  s2 += image[img_cy  +i][img_cx_1  ];
    if      (mb_available_up)                       s  = (s1   +2) >> 2;
    else if (mb_available_left)                     s  = (s2   +2) >> 2;
    break;
  case 2: //===== BOTTOM LEFT =====
    if      (mb_available_left)  for (i=0;i<4;i++)  s3 += image[img_cy_4+i][img_cx_1  ];
    else if (mb_available_up)    for (i=0;i<4;i++)  s0 += image[img_cy_1  ][img_cx  +i];
    if      (mb_available_left)                     s  = (s3   +2) >> 2;
    else if (mb_available_up)                       s  = (s0   +2) >> 2;
    break;
  case 3: //===== BOTTOM RIGHT =====
    if      (mb_available_up)    for (i=0;i<4;i++)  s1 += image[img_cy_1  ][img_cx_4+i];
    if      (mb_available_left)  for (i=0;i<4;i++)  s3 += image[img_cy_4+i][img_cx_1  ];
    if      (mb_available_up && mb_available_left)  s  = (s1+s3+4) >> 3;
    else if (mb_available_up)                       s  = (s1   +2) >> 2;
    else if (mb_available_left)                     s  = (s3   +2) >> 2;
    break;
  }
  
  //===== prediction =====
  for (j=block_y; j<block_y+4; j++)
  for (i=block_x; i<block_x+4; i++)
  {
    img->mpr[i][j] = s;
  }
}



/*!
 ************************************************************************
 * \brief
 *    Predict one chroma 4x4 block
 ************************************************************************
 */
void
ChromaPrediction4x4 (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  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_frame) // <-- reference frame for forward prediction (if (<0) -> intra prediction)
{
  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_c_x + block_x;
  int  pic_pix_y = img->pix_c_y + block_y;
  int* fpred     = fw_pred;
  int* bpred     = bw_pred;
  int  direct    = (fw_mode == 0 && bw_mode == 0);

  //===== INTRA PREDICTION =====
  if (fw_ref_frame < 0)
  {
    IntraChromaPrediction4x4 (uv, block_x, block_y);
    return;
  }

  //===== INTER PREDICTION =====
  if (fw_mode || direct)
  {
    OneComponentChromaPrediction4x4 (fw_pred, pic_pix_x, pic_pix_y, img->all_mv , fw_ref_frame, fw_mode, uv);
  }
  if (bw_mode || direct)
  {
    OneComponentChromaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, img->all_bmv,           -1, bw_mode, uv);
  }

  if (direct || (fw_mode && bw_mode))
  {
    for (j=block_y; j<block_y4; j++)
    for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = (int)((*fpred++ + *bpred++) / 2.0 + 0.5);
  }
  else if (fw_mode)
  {
    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
 *    Chroma residual coding for an macroblock
 ************************************************************************
 */
void ChromaResidualCoding (int* cr_cbp)
{
  int   uv, block8, block_y, block_x, j, i;
  int   fw_mode, bw_mode, refframe;

  for (*cr_cbp=0, uv=0; uv<2; uv++)
  {
    //===== prediction of chrominance blocks ===d==
    block8 = 0;
    for (block_y=0; block_y<8; block_y+=4)
    for (block_x=0; block_x<8; block_x+=4, block8++)
    {
      SetModesAndRefframe (block8, &fw_mode, &bw_mode, &refframe);

      ChromaPrediction4x4 (uv, block_x, block_y, fw_mode, bw_mode, refframe);
    }

    //===== calculation of displaced frame difference =====
    for (j=0; j<8; j++)
    for (i=0; i<8; i++)
    {
      img->m7[i][j] = imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i] - img->mpr[i][j];
    }

    //===== DCT, Quantization, inverse Quantization, IDCT, and Reconstruction =====
    if (img->types!=SP_IMG || IS_INTRA (&img->mb_data[img->current_mb_nr]))
      *cr_cbp=dct_chroma   (uv,*cr_cbp);
    else
      *cr_cbp=dct_chroma_sp(uv,*cr_cbp);
  }

  //===== update currMB->cbp =====
  img->mb_data[img->current_mb_nr].cbp += ((*cr_cbp)<<4);  
}



/*!
 ************************************************************************
 * \brief
 *    Set reference frame information in global arrays
 *    depending on mode decision. Used for motion vector prediction.
 ************************************************************************
 */
void SetRefFrameInfo(int refframe, int bwrefframe)
{
  int i,j;

  if (img->type!=B_IMG)
  {
      for (j=0; j<4; j++)
      for (i=0; i<4; i++)

⌨️ 快捷键说明

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