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

📄 macroblock.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:

  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++)
    {
      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* 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_IMG && 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;
    }
    else if (currMB->b8pdir[b8]==1)
    {
      *fw_ref   = 0;
      *bw_ref   = bw_refarr[block_y+j][img->block_x+i];
      *fw_mode  = 0;
      *bw_mode  = currMB->b8mode[b8];
    }
    else
    {
      *fw_ref   = fw_refarr[block_y+j][img->block_x+i];
      *bw_ref   = bw_refarr[block_y+j][img->block_x+i];
      *fw_mode  = currMB->b8mode[b8];
      *bw_mode  = currMB->b8mode[b8];

      if (currMB->b8mode[b8]==0) // direct
      {
        if (img->type==BS_IMG)
        {
            *fw_ref = 0;
            *bw_ref = 0;
        }
        else if (img->type==B_IMG)
        {
          *fw_ref = max(0,frefarr[block_y+j][img->block_x+i]);
          *bw_ref = 0;
        }
        else
        {
          *fw_ref = max(0,frefarr[block_y+j][img->block_x+i]);
          *bw_ref = 0;
        }
      }
    }
  }
}


/*!
 ************************************************************************
 * \brief
 *    Residual Coding of a Luma macroblock (not for intra)
 ************************************************************************
 */
void
LumaResidualCoding ()
{
  int i,j,block8x8,b8_x,b8_y;
  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++)
  {
    int bw_ref;
    SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe, &bw_ref);

    sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,
                                           fw_mode, bw_mode, refframe, bw_ref);
  }

  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(block8x8=0;block8x8<4;block8x8++)
       {
         b8_x=(block8x8&1)<<3;
         b8_y=(block8x8&2)<<2;
         for (i=0;i<8;i+=4)
           for (j=0;j<8;j+=4)
             copyblock_sp(b8_x+i,b8_y+j);
       }
     }
   }
}



//add by zdd 
void calc2x2(byte *ref,int imgcw,_int16 coff[],_int16 *output)
{
	_int64 f32=0x0000002000000020;	
	_asm
	{
		mov esi,coff			
		mov eax,ref		
		mov ecx,output			
		movq  mm6,[esi]
		pxor  mm7,mm7			
		movq mm5,mm6
		PUNPCKHDQ mm6,mm6 
		PUNPCKLDQ mm5,mm5 	 

		
		//计算u第一行
		movd mm0,[eax]				
		PUNPCKLBW mm0,mm7	
		PSHUFW mm0,mm0,148	
		add eax,imgcw		
		PMADDWD mm0,mm5		

		movd mm1,[eax]		
		PUNPCKLBW mm1,mm7
		PSHUFW mm1,mm1,148
		movq mm2,mm1
		PMADDWD mm1,mm6		

		paddd  mm0,mm1	
		paddd  mm0,f32	
		psrad  mm0,6		
		PACKSSDW mm0,mm7			
		//计算u第二行
		add eax,imgcw
		PMADDWD mm2,mm5			

		movd mm3,[eax]		
		PUNPCKLBW mm3,mm7
		PSHUFW mm3,mm3,148
		PMADDWD mm3,mm6

		paddd  mm2,mm3		
		paddd  mm2,f32		
		psrad  mm2,6
		PACKSSDW mm2,mm7
		
		/*PEXTRW esi,mm2,0
		PEXTRW edi,mm0,1
		PINSRW mm0,esi,1
		PINSRW mm2,edi,0*/		
		movd [ecx],mm0
		movd [ecx+8],mm2


		emms
	}
}
/*!
 ************************************************************************
 * \brief
 *    Predict one component of a chroma 4x4 block
 ************************************************************************
 */
 //zdd
void
OneComponentChromaPrediction4x4 (_int16*     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     incr;
  int*    mvb;
  int     refframe  = (ref<0 ?      0 :    ref);
  pel_t** refimage;
  int     je        = pix_c_y + 4;
  int     ie        = pix_c_x + 4;
  int     f1        = 8 , f2=f1-1, f3=f1*f1, f4=f3>>1;
  int     s1        = 3;
  int     img_pic_c_y = img->pix_c_y;
  int   scale   = 1;
  int chroma_shift = 0;
  int parity_of_mb;
  int field_mode;
  
  int imgcw=img->width_cr;  
  int imgcwp32=imgcw+32;
  int imgw=img->width;  
  _int16 coff[4];
  _int16 buff[16];
  int inti,intj;
  //int      oldref=ref;

  incr      = 0;//(ref==-1 ? (!parity_of_mb&&field_mode): direct_mode ? (!parity_of_mb&&field_mode) : field_mode) ;
  
  if(img->type==B_IMG) ref += 1+incr;
  refimage  = mcef [ref][uv];
   
  //OneComponentChromaPrediction4x41(buff,pix_c_x,pix_c_y,mv,oldref,blocktype,uv);
  for (j=0; j<4; j+=2)
  for (i=0; i<4; i+=2)
  {
    mvb  = mv [(i+pix_c_x-img->pix_c_x)>>1][(j+pix_c_y-img_pic_c_y)>>1][refframe][blocktype];
    ii   = ((i+pix_c_x)<<s1) + mvb[0];
    jj   = ((j+pix_c_y)<<s1) + mvb[1];
    jj   += chroma_shift;

    /*ii0  = max (0, min (img->width_cr -1, ii>>s1     ));
    jj0  = max (0, min (img->height_cr/scale-1, jj>>s1     ));    // For MB level field/frame -- scale chroma height by 2
    ii1  = max (0, min (img->width_cr -1, (ii+f2)>>s1));
    jj1  = max (0, min (img->height_cr/scale-1, (jj+f2)>>s1));*/
	//整数部分
	inti=(ii>>3);
	intj=(jj>>3);								
	inti=(ii>>3);
	intj=(jj>>3);								
	inti=inti>=-14?inti:-14;
	intj=intj>=-14?intj:-14;
	inti=inti<=img->width_cr+3?inti:img->width_cr+3;
	intj=intj<=img->height_cr+3?intj:img->height_cr+3;
	
	//非整数部分
	if1=(ii & 7);
	jf1=(jj & 7);
	//插值系数
	if0=8-if1;
	jf0=8-jf1;
	coff[0]=multtab[if0][jf0];
	coff[1]=multtab[if1][jf0];
	coff[2]=multtab[if0][jf1];
	coff[3]=multtab[if1][jf1];
	calc2x2(&(refimage[intj][inti]),imgcwp32,coff,&(mpred[j*4+i]));
  }

   
}



/*!
 ************************************************************************
 * \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 mode = img->mb_data[img->current_mb_nr].c_ipred_mode;
  int i, j;

  //===== prediction =====
  for (j=block_y; j<block_y+4; j++)
  for (i=block_x; i<block_x+4; i++)
  {
    img->mpr[i][j] = img->mprr_c[uv][mode][i][j];
  }
}



/*!
 ************************************************************************
 * \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)
                     int  bw_ref_frame) // <-- reference frame for backward prediction 
{
  static _int16 fw_pred[16];
  static _int16 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;
  _int16* fpred     = fw_pred;
  _int16* bpred     = bw_pred;
  
  int  direct    = (fw_mode == 0 && bw_mode == 0 && (img->type == B_IMG ));

  int  skipped   = (fw_mode == 0 && bw_mode == 0 && (img->type != B_IMG ));
  int***** fmv_array = img->all_mv;
  int***** bmv_array = img->all_bmv;
  int fw_ref_idx, bw_ref_idx;
  //zdd
  _int16 *pt1=&(img->mpr[block_x][block_y]);  
  static _int64 f1=0x0001000100010001;
  static _int64 zero=0;
  //end



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

  //===== INTRA PREDICTION =====
  if (fw_ref_frame < 0)
  {
    IntraChromaPrediction4x4 (uv, block_x, block_y);
    return;
  }
  
  if ( direct)
  {    
    fw_ref_frame = fwdir_refFrArr[pic_pix_y/2][pic_pix_x/2];
    bw_ref_frame = bwdir_refFrArr[pic_pix_y/2][pic_pix_x/2];
  }
  
    fw_ref_idx = fw_ref_frame;
    bw_ref_idx = bw_ref_frame;
  


  //===== INTER PREDICTION =====
  if (fw_mode || (direct&&fw_ref_frame!=-1 ) || skipped)
  {
    OneComponentChromaPrediction4x4 (fw_pred, pic_pix_x, pic_pix_y, fmv_array , fw_ref_frame, fw_mode, uv);
  }
  if (bw_mode || (direct&&bw_ref_frame!=-1 ))
  {
   
      OneComponentChromaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, bmv_array,           -1, bw_mode, uv);
  }
  if (direct || (fw_mode && bw_mode))
    {
      if ( direct)
      {        
		  if (fw_ref_frame ==-1)
		  {
			  _asm
			  {
				  mov edi,pt1
				  mov esi,bpred
				  //pxor mm7,mm7
				  movq mm0,[esi]				  			  
				  movq mm1,[esi+8]				  			  

⌨️ 快捷键说明

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