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

📄 macroblock.c

📁 AVS编解码是学习AVS程序开发的入门资料,可以帮助初学者获得很多的收获.
💻 C
📖 第 1 页 / 共 5 页
字号:
        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.
    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;
  
  // 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)
  {
    int remove_prediction = currMB->slice_nr != mb_data[mb_nr-1].slice_nr;
    // 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) 
  {
    int remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width].slice_nr;
    // 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)
  {
    int remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width-1].slice_nr;
    
    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(currMB->slice_nr == mb_data[mb_nr-mb_width+1].slice_nr)
      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;

  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
    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; 
  }
}

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

void interpret_mb_mode_B(struct img_par *img)
{
  static const int offset2pdir16x16[12]   = {0, 0, 1, 2, 0,0,0,0,0,0,0,0};
  static const int offset2pdir16x8[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},{1,0},
  {0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2},{0,0}};
  static const int offset2pdir8x16[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},
  {1,0},{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2}};
  
  const int ICBPTAB[6] = {0,16,32,15,31,47};
  Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
  
  int i, mbmode;
  int mbtype  = currMB->mb_type;
  int *b8mode = currMB->b8mode;
  int *b8pdir = currMB->b8pdir;
  
  //--- set mbtype, b8type, and b8pdir ---
	if (mbtype==0)       // direct
	{
		mbmode=0;       for(i=0;i<4;i++) {b8mode[i]=0;          b8pdir[i]=2; }
	}
	else if (/*mbtype==23 qhg */ mbtype>=23) // intra4x4
	{
		currMB->cbp=NCBP[mbtype-23][0]; // qhg
		mbmode=I4MB;    for(i=0;i<4;i++) {b8mode[i]=IBLOCK;     b8pdir[i]=-1; }
	}
	else if (mbtype==22) // 8x8(+split)
	{
		mbmode=P8x8;       // b8mode and pdir is transmitted in additional codewords
	}
	else if (mbtype<4)   // 16x16
	{
		mbmode=1;      
		for(i=0;i<4;i++) {b8mode[i]=1;          b8pdir[i]=offset2pdir16x16[mbtype]; }
	}
	else if (mbtype%2==0) // 16x8
	{
	        mbmode=2;       
		for(i=0;i<4;i++) {b8mode[i]=2;          b8pdir[i]=offset2pdir16x8 [mbtype][i/2]; }
	}
	else
	{
		mbmode=3;       
	 	for(i=0;i<4;i++) {b8mode[i]=3;          b8pdir[i]=offset2pdir8x16 [mbtype][i%2]; }
	}
		
	
	currMB->mb_type = mbmode;
}

/*
*************************************************************************
* Function:init macroblock I and P frames
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void init_macroblock(struct img_par *img)
{
  int i,j;
  Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
	
  img->mv[img->block_x+4][img->block_y][2]=img->number;
	
  for (i=0;i<2;i++)

⌨️ 快捷键说明

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