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

📄 macroblock.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:
            m7 = &img->m7[0][j][block_x]; 
            for (i = 0; i < 4; i++)
            {
              *m7++ = *imgOrg++ - *imgPred++;
            }
          }
        }

        if ( active_sps->chroma_format_idc==YUV444 && (IS_INDEPENDENT(input)==0) ) 
        {
          for (uv=0; uv<2; uv++)
          {
            select_plane((ColorPlane) (uv + 1));
            ChromaPrediction (currMB, uv, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);

            for (j = block_y; j < block_y + 4; j++)
            {
              imgOrg  = &imgUV_org[uv][img->opix_y + j][pic_pix_x];    
              imgPred = &img->mpr[uv + 1][j][block_x];
              m7 = &img->m7[uv+1][j][block_x]; 
              for (i=0; i<4; i++)
              {
                *m7++ = *imgOrg++ - *imgPred++;
              }
            }
          }
          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 =====
          //select_dct(currMB);
          nonzero = pDCT_4x4 (currMB, PLANE_Y, block_x, block_y, &coeff_cost, 0);

          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( (active_sps->chroma_format_idc) == YUV444 && !IS_INDEPENDENT(input) ) 
          {
            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);
                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);
        //===== get displaced frame difference ======
        for (j=0; j<8; j++)
        {
          imgOrg = &pCurImg[pic_pix_y + j][pic_pix_x];    
          imgPred = &curr_mpr[block_y + j][block_x];
          m7 = &img->m7[0][block_y + j][block_x];
          for (i=0; i<8; i++)
          {
            *m7++ = *imgOrg++ - *imgPred++;
          }
        }

        if ( (active_sps->chroma_format_idc == YUV444) && !IS_INDEPENDENT(input) ) 
        {
          for (uv=0; uv<2; uv++)
          {
            select_plane((ColorPlane) (uv + 1));
            ChromaPrediction (currMB, uv, block_x, block_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);

            for (j=0; j<8; j++)
            {
              imgOrg = &imgUV_org[uv][pic_pix_y + j][pic_pix_x];    
              imgPred = &img->mpr[uv + 1][block_y + j][block_x];
              m7 = &img->m7[uv + 1][block_y + j][block_x];
              for (i=0; i<8; i++)
              {
                *m7++ = *imgOrg++ - *imgPred++;
              }
            }
          }
          select_plane(PLANE_Y);
        }
      }
    }

    if (img->NoResidueDirect != 1 && !skipped)
    {
      if (img->type!=SP_SLICE)
        nonzero = dct_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( (active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0) ) 
      {
        if (img->type!=SP_SLICE)
        {
          for (uv = PLANE_U; uv <= PLANE_V; uv++)
          {
            select_plane((ColorPlane) uv);
            nonzerocr[uv] = dct_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)));
    /*
    for( i = 0; i < 4; i++ )
      for( j = 0; j < 2; j++ )
        memset( img->cofAC[block8x8][i][j], 0, 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], &curr_mpr[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( (active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0) )
  {
    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)));

        for( i = 0; i < 4; i++ )
          for( j = 0; j < 2; j++ )
            memset( img->cofAC[block8x8 + 4 * uv][i][j], 0, 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->mpr[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->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->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->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->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
      list_ref_idx[1] = enc_picture->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 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 (*curr_mpr)[16] = img->mpr[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]);
    
    if(  (active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0)  ) 
    {
      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], curr_mpr[j], MB_BLOCK_SIZE * sizeof (imgpel));
    
    /*
    for(block8x8=0;block8x8<4;block8x8++)
    {
      for( i = 0; i < 4; i++ )
        for( j = 0; j < 2; j++ )
          memset( img->cofAC[block8x8][i][j], 0, 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->yuv_format == YUV444) && !IS_INDEPENDENT(input) )
  {
    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->mpr[uv][j], MB_BLOCK_SIZE * sizeof (imgpel));

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

⌨️ 快捷键说明

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