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

📄 macroblock.c

📁 AVS编解码是学习AVS程序开发的入门资料,可以帮助初学者获得很多的收获.
💻 C
📖 第 1 页 / 共 5 页
字号:
  {                           // reset vectors and pred. modes
    for(j=0;j<2;j++)
    {
        img->mv[img->block_x+i+4][img->block_y+j][0] = 0;
        img->mv[img->block_x+i+4][img->block_y+j][1] = 0;
    }
  }

  for (i=0;i<2;i++)
  {                           // reset vectors and pred. modes
    for(j=0;j<2;j++)
    {
	img->ipredmode[img->block_x+i+1][img->block_y+j+1] = -1;            //by oliver 0512
    }
  }

  // Set the reference frame information for motion vector prediction
  if (IS_INTRA (currMB))
  {
	for (j=0; j<2; j++)
		for (i=0; i<2; i++)
		{
			refFrArr[img->block_y+j][img->block_x+i] = -1;
		}
  }
  else if (!IS_P8x8 (currMB))
  {
	for (j=0; j<2; j++)
		for (i=0; i<2; i++)
		{
			refFrArr[img->block_y+j][img->block_x+i] = 0;
		}
  }
  else
  {
	for (j=0; j<2; j++)
		for (i=0; i<2; i++)
		{
			refFrArr[img->block_y+j][img->block_x+i] = (currMB->b8mode[2*j+i]==IBLOCK ? -1 : 0);
		}
  } 
}

/*
*************************************************************************
* Function:Sets mode for 8x8 block
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void SetB8Mode (struct img_par* img, Macroblock* currMB, int value, int i)
{
  static const int p_v2b8 [ 5] = {4, 5, 6, 7, IBLOCK};
  static const int p_v2pd [ 5] = {0, 0, 0, 0, -1};
  static const int b_v2b8 [14] = {0, 4, 4, 4, 5, 6, 5, 6, 5, 6, 7, 7, 7, IBLOCK};
  static const int b_v2pd [14] = {2, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 2, -1};
  
  if (img->type==B_IMG)
  {
    currMB->b8mode[i]   = b_v2b8[value];
    currMB->b8pdir[i]   = b_v2pd[value];
  }
  else
  {
    currMB->b8mode[i]   = p_v2b8[value];
    currMB->b8pdir[i]   = p_v2pd[value];
  }
  
}

/*
*************************************************************************
* Function:Get the syntax elements from the NAL
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

int read_one_macroblock(struct img_par *img,struct inp_par *inp)
{
  int i,j;
  	 
  SyntaxElement currSE;
  Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
  int  img_block_y;
  int  real_mb_type;
   int fixqp;          //add by wuzhongmou 200612
   fixqp = (fixed_picture_qp|| fixed_slice_qp);  //add by wuzhongmou 200612

  for(i=0;i<8;i++)
    for(j=0;j<8;j++)
    {
      img->m8[0][i][j]=0;
      img->m8[1][i][j]=0;
    }
    
  currMB->qp = img->qp ;
  currSE.type = SE_MBTYPE;
  currSE.mapping = linfo_ue;
  
	currMB->mb_type_2= 0;	

  if(img->type == I_IMG) 
  {
		currMB->mb_type = 0;
  } 
  else if(skip_mode_flag)
  {
    if(img->cod_counter == -1)
    {

#if TRACE
      strncpy(currSE.tracestring, "MB runlength", TRACESTRING_SIZE);
#endif

      readSyntaxElement_UVLC(&currSE,inp);
      img->cod_counter = currSE.value1;
    }

    if (img->cod_counter==0)
    {
#if TRACE
      strncpy(currSE.tracestring, "MB Type", TRACESTRING_SIZE);
#endif

      readSyntaxElement_UVLC(&currSE,inp);

      if(img->type == P_IMG)
        currSE.value1++;
      
      currMB->mb_type = currSE.value1;
      img->cod_counter--;
    } 
    else
    {
      img->cod_counter--;
      currMB->mb_type = 0;  // !! skip mode shenyanfei 
    }
  } 
  else 
  {

#if TRACE
	strncpy(currSE.tracestring, "MB Type", TRACESTRING_SIZE);
#endif
		
	readSyntaxElement_UVLC(&currSE,inp);
	
	if(img->type == P_IMG)
		currSE.value1++;
	
	currSE.value1--;
	real_mb_type = currSE.value1;
	if(currSE.value1<0)
		currSE.value1 = 0;
	currMB->mb_type = currSE.value1;
	img->cod_counter--;		
  }
  
  if ((img->type==P_IMG ))      // inter frame
    interpret_mb_mode_P(img);
  else if (img->type==I_IMG)                                  // intra frame
    interpret_mb_mode_I(img);
  else if ((img->type==B_IMG))  // B frame
    interpret_mb_mode_B(img);

	//====== READ 8x8 SUB-PARTITION MODES (modes of 8x8 blocks) and Intra VBST block modes ======
  if (IS_P8x8 (currMB))
  {
    currSE.type    = SE_MBTYPE;    
		
    if(img->type!=P_IMG)
    {
      for (i=0; i<4; i++)
      {
        currSE.mapping = linfo_ue;
        
#if TRACE
        strncpy(currSE.tracestring, "8x8 mode", TRACESTRING_SIZE);
#endif
        
        //mb_part_type is fix length coding(fix length equal 2)!!   jlzheng    7.22
        assert (currStream->streamBuffer != NULL);
        currSE.len = 2;
        readSyntaxElement_FLC (&currSE);
		//  END
        SetB8Mode (img, currMB, currSE.value1, i);
      }
    }
    else
    {
      currSE.value1 = 0;
      for (i=0; i<4; i++)
      {
        SetB8Mode (img, currMB, currSE.value1, i);
      }
    }
  }


   
  //! TO for Error Concelament
  //! If we have an INTRA Macroblock and we lost the partition
  //! which contains the intra coefficients Copy MB would be better 
  //! than just a grey block.
  //! Seems to be a bit at the wrong place to do this right here, but for this case 
  //! up to now there is no other way.//Lou

  //--- init macroblock data ---
  if (img->type==B_IMG) 
    init_macroblock_Bframe(img);
  else
    init_macroblock       (img);
  
	if (IS_INTRA(currMB))
	{
		for (i=0;i<5;i++)
			read_ipred_block_modes(img,inp,i);
	}

	if (skip_mode_flag)
	{
		if (IS_DIRECT (currMB) && img->cod_counter >= 0)
		{
			int i, j, iii, jjj;
			currMB->cbp = 0;
			
			for (i=0;i<BLOCK_SIZE;i++)
			{ // reset luma coeffs
				for (j=0;j<BLOCK_SIZE;j++)
					for(iii=0;iii<BLOCK_SIZE;iii++)
						for(jjj=0;jjj<BLOCK_SIZE;jjj++)
							img->cof[i][j][iii][jjj]=0;
			}
			
			for (j=4;j<6;j++)
			{ // reset chroma coeffs
				for (i=0;i<4;i++)
					for (iii=0;iii<4;iii++)
						for (jjj=0;jjj<4;jjj++)
							img->cof[i][j][iii][jjj]=0;
			}
		
			return DECODE_MB;
		}
	}
	else
	{
		if (img->type==B_IMG&& real_mb_type<0)
		{
			int i, j, iii, jjj;
			currMB->cbp = 0;
			
			for (i=0;i<BLOCK_SIZE;i++)
			{ // reset luma coeffs
				for (j=0;j<BLOCK_SIZE;j++)
					for(iii=0;iii<BLOCK_SIZE;iii++)
						for(jjj=0;jjj<BLOCK_SIZE;jjj++)
							img->cof[i][j][iii][jjj]=0;
			}
			
			for (j=4;j<6;j++)
			{ // reset chroma coeffs
				for (i=0;i<4;i++)
					for (iii=0;iii<4;iii++)
						for (jjj=0;jjj<4;jjj++)
							img->cof[i][j][iii][jjj]=0;
			}

			return DECODE_MB;
		}
	}
  
	if (IS_COPY (currMB)) //keep last macroblock
	{
		int i, j, iii, jjj, pmv[2];
		int ***tmp_mv         = img->mv;
		int mb_available_up   = (img->mb_y == 0)  ? 0 : (currMB->slice_nr == mb_data[img->current_mb_nr-img->width/16].slice_nr);
		int mb_available_left = (img->mb_x == 0)  ? 0 : (currMB->slice_nr == mb_data[img->current_mb_nr-1].slice_nr);
		int zeroMotionAbove   = !mb_available_up  ? 1 : refFrArr[img->block_y-1][img->block_x]  == 0 && tmp_mv[4+img->block_x  ][img->block_y-1][0] == 0 && tmp_mv[4+img->block_x  ][img->block_y-1][1] == 0 ? 1 : 0;
		int zeroMotionLeft    = !mb_available_left? 1 : refFrArr[img->block_y][img->block_x-1]  == 0 && tmp_mv[4+img->block_x-1][img->block_y  ][0] == 0 && tmp_mv[4+img->block_x-1][img->block_y  ][1] == 0 ? 1 : 0;
		
		currMB->cbp = 0;
		
		for (i=0;i<BLOCK_SIZE;i++)
		{ // reset luma coeffs
			for (j=0;j<BLOCK_SIZE;j++)
				for(iii=0;iii<BLOCK_SIZE;iii++)
					for(jjj=0;jjj<BLOCK_SIZE;jjj++)
						img->cof[i][j][iii][jjj]=0;
		}
		for (j=4;j<6;j++)
		{ // reset chroma coeffs
			for (i=0;i<4;i++)
				for (iii=0;iii<4;iii++)
					for (jjj=0;jjj<4;jjj++)
						img->cof[i][j][iii][jjj]=0;
		}
		
		
		img_block_y   = img->block_y; 
		
		if (zeroMotionAbove || zeroMotionLeft)
		{
			for(i=0;i<2;i++)
				for(j=0;j<2;j++)
				{
					img->mv[img->block_x+i+BLOCK_SIZE][img->block_y+j][0] = 0;
					img->mv[img->block_x+i+BLOCK_SIZE][img->block_y+j][1] = 0;
				}
		}
		else
		{
			SetMotionVectorPredictor (img, pmv, pmv+1, 0, refFrArr, img->mv, 0, 0, 16, 16, 0, 0);//Lou 1016
			for(i=0;i<2;i++)
				for(j=0;j<2;j++)
				{
					img->mv[img->block_x+i+BLOCK_SIZE][img_block_y+j][0] = pmv[0];
					img->mv[img->block_x+i+BLOCK_SIZE][img_block_y+j][1] = pmv[1];
				}
		}
		
		for (j=0; j<2;j++)
			for (i=0; i<2;i++)
			{
				refFrArr_frm[img->block_y+j][img->block_x+i] = 0;
			}
			
		return DECODE_MB;
	}

	readReferenceIndex(img, inp); 
	readMotionVector (img, inp);
	
	// !! start shenyanfei cjw
	//if((!IS_INTRA(currMB)) && (img->slice_weighting_flag == 1) && (img->mb_weighting_flag == 0)){ //cjw 20051219
	//if((!IS_INTRA(currMB)) && (img->slice_weighting_flag == 1) && (img->mb_weighting_flag == 1)&&(!IS_COPY(currMB))&&(!IS_DIRECT(currMB))){ //cjw20051230 for skip MB 
	if((!IS_INTRA(currMB)) && (img->slice_weighting_flag == 1) && (img->mb_weighting_flag == 1)&&(!IS_COPY(currMB))){ //cjw20060321 skip no WP, direct WP
		img->weighting_prediction = u_v(1,"MB weighting_prediction");  //cjw 20051219
	}
	// !! end shenyanfei cjw

	// read CBP if not new intra mode
  if (!(IS_COPY (currMB) || (IS_DIRECT (currMB) && img->cod_counter >= 0)))
  {

    if (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB )   currSE.type = SE_CBP_INTRA;
    else                        currSE.type = SE_CBP_INTER;
    
    if (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB)  
      currSE.mapping = linfo_cbp_intra;
    else                       
      currSE.mapping = linfo_cbp_inter;
    
    
#if TRACE
    snprintf(currSE.tracestring, TRACESTRING_SIZE, "CBP");
#endif
    
    if(img->type==I_IMG||IS_INTER(currMB)) // qhg
	{
		currSE.golomb_maxlevels = 0;
		readSyntaxElement_UVLC(&currSE,inp);
		currMB->cbp = currSE.value1;
    }
    // Delta quant only if nonzero coeffs
    if (!fixqp&&currMB->cbp !=0)
    {
      if (IS_INTER (currMB))  currSE.type = SE_DELTA_QUANT_INTER;
      else                    currSE.type = SE_DELTA_QUANT_INTRA;
      
      currSE.mapping = linfo_se;
      
#if TRACE
      snprintf(currSE.tracestring, TRACESTRING_SIZE, "Delta quant ");
#endif
      readSyntaxElement_UVLC(&currSE,inp);
      currMB->delta_quant = currSE.value1;
      img->qp= (img->qp-MIN_QP+currMB->delta_quant+(MAX_QP-MIN_QP+1))%(MAX_QP-MIN_QP+1)+MIN_QP; //discussed by cjw AVS Zhuhai 20070123
    }

	if (fixqp)
	{
		currMB->delta_quant = 0;
		img->qp= (img->qp-MIN_QP+currMB->delta_quant+(MAX_QP-MIN_QP+1))%(MAX_QP-MIN_QP+1)+MIN_QP;
		
	}

  }
  // read CBP and Coeffs  ***************************************************************
  readCBPandCoeffsFromNAL (img,inp);
  
  return DECODE_MB;
}

void read_ipred_block_modes(struct img_par *img,struct inp_par *inp,int b8)
{
  int bi,bj,dec;
  SyntaxElement currSE;
  Macroblock *currMB;
  int j2;
  int mostProbableIntraPredMode;
  int upIntraPredMode;
  int leftIntraPredMode;
  int IntraChromaPredModeFlag;
	int MBRowSize = img->width / MB_BLOCK_SIZE;
  
  currMB=mb_data+img->current_mb_nr;//current_mb_nr;
  IntraChromaPredModeFlag = IS_INTRA(currMB);
  
  currSE.type = SE_INTRAPREDMODE;
#if TRACE
  strncpy(currSE.tracestring, "Ipred Mode", TRACESTRING_SIZE);

⌨️ 快捷键说明

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