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

📄 macroblock.c

📁 avs-s最新代码,包括编码器和解码器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  }
  else if(blockshape_x == 16 && blockshape_y == 8)
  {
    if(mb_y == 0)
    {
      if(rFrameU == ref_frame)
        mvPredType = MVPRED_U;
    }
    else
    {
      if(rFrameL == ref_frame)
        mvPredType = MVPRED_L;
    }
  }
  
  #define MEDIAN(a,b,c)  (a + b + c - min(a, min(b, c)) - max(a, max(b, c)));

  for (hv=0; hv < 2; hv++)
  {
    mva[hv] = mv_a = block_available_left    ? tmp_mv[4+pic_block_x-1             ][pic_block_y][hv] : 0;
    mvb[hv] = mv_b = block_available_up      ? tmp_mv[4+pic_block_x               ][pic_block_y-1][hv] : 0;
    mv_d = block_available_upleft  ? tmp_mv[4+pic_block_x-1][pic_block_y-1][hv] : 0;
    mvc[hv] = mv_c = block_available_upright ? tmp_mv[4+pic_block_x+blockshape_x/8][pic_block_y-1][hv] : mv_d;
    //--- Yulj 2004.07.14
    // mv_a, mv_b... are not scaled.

#ifdef half_pixel_compensation
	  if(!img->picture_structure && hv==1)  
	  {
			mva[hv] = scale_motion_vector_pixel(mva[hv], ref_frame, rFrameL, smbtypecurr, smbtypeL, pic_block_y-off_y, pic_block_y, ref, direct_mv);
			mvb[hv] = scale_motion_vector_pixel(mvb[hv], ref_frame, rFrameU, smbtypecurr, smbtypeU, pic_block_y-y_up, pic_block_y, ref, direct_mv);
			mv_d = scale_motion_vector_pixel(mv_d, ref_frame, rFrameUL, smbtypecurr, smbtypeUL, pic_block_y-y_upleft, pic_block_y, ref, direct_mv);
			mvc[hv] = block_available_upright ? scale_motion_vector_pixel(mvc[hv], ref_frame, rFrameUR, smbtypecurr, smbtypeUR, pic_block_y-y_upright, pic_block_y, ref, direct_mv): mv_d;    
	  }
	  else
#endif
	  {
    mva[hv] = scale_motion_vector(mva[hv], ref_frame, rFrameL, smbtypecurr, smbtypeL, pic_block_y-off_y, pic_block_y, ref, direct_mv);
    mvb[hv] = scale_motion_vector(mvb[hv], ref_frame, rFrameU, smbtypecurr, smbtypeU, pic_block_y-y_up, pic_block_y, ref, direct_mv);
    mv_d = scale_motion_vector(mv_d, ref_frame, rFrameUL, smbtypecurr, smbtypeUL, pic_block_y-y_upleft, pic_block_y, ref, direct_mv);
    mvc[hv] = block_available_upright ? scale_motion_vector(mvc[hv], ref_frame, rFrameUR, smbtypecurr, smbtypeUR, pic_block_y-y_upright, pic_block_y, ref, direct_mv): mv_d;
	  }
    
    switch (mvPredType)
    {
    case MVPRED_MEDIAN:

      if(hv == 1){
          //  jlzheng 7.2
          // !! for A 

          mva[2] = abs(mva[0] - mvb[0])	+ abs(mva[1] - mvb[1]);
          // !! for B

	  mvb[2] = abs(mvb[0] - mvc[0]) + abs(mvb[1] - mvc[1]);
          // !! for C

          mvc[2] = abs(mvc[0] - mva[0])	+ abs(mvc[1] - mva[1]);
          
          pred_vec = MEDIAN(mva[2],mvb[2],mvc[2]);
          
          if(pred_vec == mva[2]){
            *pmv_x = mvc[0];
            *pmv_y = mvc[1];
          }
          else if(pred_vec == mvb[2]){
            *pmv_x = mva[0];
            *pmv_y = mva[1];
          }
          else{
            *pmv_x = mvb[0];
            *pmv_y = mvb[1];
          }   //  END
          

      }
      break;
    case MVPRED_L:
      pred_vec = mv_a;
      break;
    case MVPRED_U:
      pred_vec = mv_b;
      break;
    case MVPRED_UR:
      pred_vec = mv_c;
      break;
    default:
      break;
    }
    
    
    if(mvPredType != MVPRED_MEDIAN){
      if (hv==0)
        *pmv_x = pred_vec;
      else
        *pmv_y = pred_vec;
    }
  }

#undef MEDIAN
}

/*
*************************************************************************
* Function:Checks the availability of neighboring macroblocks of
     the current macroblock for prediction and context determination;
     marks the unavailable MBs for intra prediction in the
     ipredmode-array by -1. Only neighboring MBs in the causal
     past of the current MB are checked.
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void CheckAvailabilityOfNeighbors(struct img_par *img)
{
  int i,j;
  const int mb_width = img->width/MB_BLOCK_SIZE;
  const int mb_nr = img->current_mb_nr;
  Macroblock *currMB = &mb_data[mb_nr];
  int check_value;
  int remove_prediction;  //added by mz, 2008.04
  
  // mark all neighbors as unavailable
  for (i=0; i<3; i++)
    for (j=0; j<3; j++)
      mb_data[mb_nr].mb_available[i][j]=NULL;

  mb_data[mb_nr].mb_available[1][1]=currMB; // current MB
    
  // Check MB to the left
  if(img->pix_x >= MB_BLOCK_SIZE)
  {
    if(!slice_set_enable)  //added by mz, 2008.04
	  remove_prediction = currMB->slice_nr != mb_data[mb_nr-1].slice_nr;
	else
      remove_prediction = currMB->slice_set_index != mb_data[mb_nr-1].slice_set_index;

    // upper blocks
    if (remove_prediction)
    {
      img->ipredmode[img->block_x][img->block_y+1] = -1;
      img->ipredmode[img->block_x][img->block_y+2] = -1;
    }
    if (!remove_prediction)
    {
      currMB->mb_available[1][0]=&(mb_data[mb_nr-1]);
    }
  }
  
  // Check MB above
  check_value =  (img->pix_y >= MB_BLOCK_SIZE);
  if(check_value) 
  {
	  if(!slice_set_enable)   //added by mz, 2008.04
		  remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width].slice_nr;
	  else
		  remove_prediction = currMB->slice_set_index != mb_data[mb_nr-mb_width].slice_set_index;

    // upper blocks
    if (remove_prediction)
    {
      img->ipredmode[img->block_x+1][img->block_y] = -1;
      img->ipredmode[img->block_x+2][img->block_y] = -1;
    }
    
    if (!remove_prediction)
    {
      currMB->mb_available[0][1]=&(mb_data[mb_nr-mb_width]);
    }
  }
  
  // Check MB left above
  if(img->pix_y >= MB_BLOCK_SIZE && img->pix_x >= MB_BLOCK_SIZE)
  {
    if(!slice_set_enable)  //added by mz, 2008.04
	  remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width-1].slice_nr;
	else
      remove_prediction = currMB->slice_set_index != mb_data[mb_nr-mb_width-1].slice_set_index;
    
    if (remove_prediction)
    {
      img->ipredmode[img->block_x][img->block_y] = -1;
    }
    if (!remove_prediction)
    {
      currMB->mb_available[0][0]=&(mb_data[mb_nr-mb_width-1]);
    }
  }
  
  // Check MB right above
  if(img->pix_y >= MB_BLOCK_SIZE && img->pix_x < (img->width-MB_BLOCK_SIZE ))
  {
	  if(!slice_set_enable)   //added by mz, 2008.04
	  {
		  if(currMB->slice_nr == mb_data[mb_nr-mb_width+1].slice_nr)
			  currMB->mb_available[0][2]=&(mb_data[mb_nr-mb_width+1]);
	  }
	  else
	  {	  
		  if(currMB->slice_set_index == mb_data[mb_nr-mb_width+1].slice_set_index)
			  currMB->mb_available[0][2]=&(mb_data[mb_nr-mb_width+1]);
	  }
  }
}

void set_MB_parameters (struct img_par *img,struct inp_par *inp, int mb)
{
  const int number_mb_per_row = img->width / MB_BLOCK_SIZE ;
  const int mb_nr = img->current_mb_nr;
  Macroblock *currMB = &mb_data[mb_nr];

  img->mb_x = mb % number_mb_per_row;
  img->mb_y = mb / number_mb_per_row;

  // Define vertical positions
  img->block8_y= img->mb_y * BLOCK_SIZE/2;
  img->block_y = img->mb_y * BLOCK_SIZE/2;      // vertical luma block position
  img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   // vertical luma macroblock position
  img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; // vertical chroma macroblock position

  // Define horizontal positions
  img->block8_x= img->mb_x * BLOCK_SIZE/2;
  img->block_x = img->mb_x * BLOCK_SIZE/2;        // luma block
  img->pix_x   = img->mb_x * MB_BLOCK_SIZE;     // luma pixel
  img->pix_c_x   = img->mb_x * MB_BLOCK_SIZE/2; // chroma pixel

}

/*
*************************************************************************
* Function:initializes the current macroblock
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/


void start_macroblock(struct img_par *img,struct inp_par *inp)
{
  int i,j,k,l;
  Macroblock *currMB;   // intialization code deleted, see below, StW
  
  assert (img->current_mb_nr >=0 && img->current_mb_nr < img->max_mb_nr);
  
  currMB = &mb_data[img->current_mb_nr];//GB
  
  /* Update coordinates of the current macroblock */
  img->mb_x = (img->current_mb_nr)%(img->width/MB_BLOCK_SIZE);
  img->mb_y = (img->current_mb_nr)/(img->width/MB_BLOCK_SIZE);
  
  /* Define vertical positions */
  img->block_y = img->mb_y * BLOCK_SIZE/2;      /* luma block position */
  img->block8_y = img->mb_y * BLOCK_SIZE/2;   
  img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   /* luma macroblock position */
  img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; /* chroma macroblock position */
  
  /* Define horizontal positions */
  img->block_x = img->mb_x * BLOCK_SIZE/2;      /* luma block position */
  img->block8_x = img->mb_x * BLOCK_SIZE/2;  
  img->pix_x   = img->mb_x * MB_BLOCK_SIZE;   /* luma pixel position */
  img->pix_c_x = img->mb_x * MB_BLOCK_SIZE/2; /* chroma pixel position */
  
  // If MB is next to a slice boundary, mark neighboring blocks unavailable for prediction
  CheckAvailabilityOfNeighbors(img);      // support only slice mode 0 in MBINTLC1 at this time
  
  // Reset syntax element entries in MB struct
  currMB->qp          = img->qp ;
  currMB->mb_type     = 0;
  currMB->delta_quant = 0;
  currMB->cbp         = 0;
  currMB->cbp_blk     = 0;
  currMB->c_ipred_mode= DC_PRED_8; //GB
  
  for (l=0; l < 2; l++)
    for (j=0; j < BLOCK_MULTIPLE; j++)
      for (i=0; i < BLOCK_MULTIPLE; i++)
        for (k=0; k < 2; k++)
          currMB->mvd[l][j][i][k] = 0;
        
  currMB->cbp_bits   = 0;
        
  // initialize img->m7 for ABT//Lou
  for (j=0; j<MB_BLOCK_SIZE; j++)
    for (i=0; i<MB_BLOCK_SIZE; i++)
      img->m7[i][j] = 0;
       
  for (j=0; j<BLOCK_SIZE; j++)
    for (i=0; i<BLOCK_SIZE; i++)
    {
      img->m8[0][i][j] = 0;
      img->m8[1][i][j] = 0;
    }
  
	 currMB->lf_disable = loop_filter_disable;

	 img->weighting_prediction=0;   //cjw 20051230 default value Weighting Predicition is 0
}

/*
*************************************************************************
* Function:Interpret the mb mode for P-Frames
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void interpret_mb_mode_P(struct img_par *img)
{
	int i;
	const int ICBPTAB[6] = {0,16,32,15,31,47};
	Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
	int         mbmode = currMB->mb_type;

#ifdef BACKGROUND
	if(img->typeb == BP_IMG){
		if(mbmode < 2)
		{
			currMB->mb_type = mbmode;
			for (i=0;i<4;i++)
			{
				currMB->b8mode[i]   = mbmode;
				currMB->b8pdir[i]   = 0;
			}
		}
		else{//>= 2
			currMB->cbp = NCBP[currMB->mb_type - 2][0]; // qhg  //modefy by xfwang 2004.7.29
			currMB->mb_type = I4MB;
			for (i=0;i<4;i++)
			{
				currMB->b8mode[i] = IBLOCK;
				currMB->b8pdir[i] = -1;
			}
		}
		return;
	}
#endif

	if(mbmode <4)
	{
		currMB->mb_type = mbmode;
		for (i=0;i<4;i++)
		{
			currMB->b8mode[i]   = mbmode;
			currMB->b8pdir[i]   = 0;
		}
	}
	else if(MODE_IS_P8x8)
	{
		currMB->mb_type = P8x8;
	}
	else if(/* MODE_IS_I4x4 qhg */mbmode>=5)//modefy by xfwang 2004.7.29
	{
		currMB->cbp=NCBP[currMB->mb_type-5][0]; // qhg  //modefy by xfwang 2004.7.29
		//4:0:0 WANGJP START
		if (chroma_format == 0)
		{
			currMB->cbp=NCBP_400[currMB->mb_type-5][0];
		}
		//WANGJP END
		currMB->mb_type = I4MB;
		for (i=0;i<4;i++)
		{
			currMB->b8mode[i] = IBLOCK;
			currMB->b8pdir[i] = -1;
		}
	}
	else
	{
		currMB->mb_type = I16MB;
		for (i=0;i<4;i++) {currMB->b8mode[i]=0; currMB->b8pdir[i]=-1; }
		currMB->cbp= ICBPTAB[(I16OFFSET)>>2];
	}
}

/*
*************************************************************************
* Function:Interpret the mb mode for I-Frames
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void interpret_mb_mode_I(struct img_par *img)
{
  int i;
  const int ICBPTAB[6] = {0,16,32,15,31,47};
  Macroblock *currMB   = &mb_data[img->current_mb_nr];
  int         num      =4;

  currMB->mb_type = I4MB;

  for (i=0;i<4;i++)
  {
    currMB->b8mode[i]=IBLOCK; 
    currMB->b8pdir[i]=-1; 
  }

  for (i=num;i<4;i++) 
  {
    currMB->b8mode[i]=currMB->mb_type_2==P8x8? 4 : currMB->mb_type_2; currMB->b8pdir[i]=0; 
  }
}

/*
*************************************************************************

⌨️ 快捷键说明

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