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

📄 macroblock.cpp

📁 h264编解码.用C++实现了图像的编解码功能。
💻 CPP
📖 第 1 页 / 共 5 页
字号:

  ///////////////////////////////
  // make horiz and vert prediction
  ///////////////////////////////

  for (i=0; i < BLOCK_SIZE; i++)
  {
    img->mprr[VERT_PRED][i*4] = pa;
    img->mprr[VERT_PRED][i*4+1] = pb;
    img->mprr[VERT_PRED][i*4+2] = pc;
    img->mprr[VERT_PRED][i*4+3] = pd;
    img->mprr[HOR_PRED][i]  = pi;
    img->mprr[HOR_PRED][4+i]  = pj;
    img->mprr[HOR_PRED][8+i]  = pk;
    img->mprr[HOR_PRED][12+i]  = pl;
  }

    // Mode DIAG_DOWN_LEFT_PRED
    img->mprr[DIAG_DOWN_LEFT_PRED][0] = (pa + pc + (pb<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][1] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][4] = (pb + pd + (pc<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][2] =
    img->mprr[DIAG_DOWN_LEFT_PRED][5] =
    img->mprr[DIAG_DOWN_LEFT_PRED][8] = (pc + pe + (pd<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][3] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][6] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][9] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][12] = (pd + pf + (pe<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][7] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][10] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][13] = (pe + pg + (pf<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][11] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][14] = (pf + ph + (pg<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][15] = (pg + 3*(ph) + 2) >>2;

    // Mode VERT_LEFT_PRED
    img->mprr[VERT_LEFT_PRED][0] = (pa + pb + 1) >>1;
    img->mprr[VERT_LEFT_PRED][1] = 
    img->mprr[VERT_LEFT_PRED][8] = (pb + pc + 1) >>1;
    img->mprr[VERT_LEFT_PRED][2] = 
    img->mprr[VERT_LEFT_PRED][9] = (pc + pd + 1) >>1;
    img->mprr[VERT_LEFT_PRED][3] = 
    img->mprr[VERT_LEFT_PRED][10] = (pd + pe + 1) >>1;
    img->mprr[VERT_LEFT_PRED][11] = (pe + pf + 1) >>1;
    img->mprr[VERT_LEFT_PRED][4] = (pa + (pb<<1) + pc + 2) >>2;
    img->mprr[VERT_LEFT_PRED][5] = 
    img->mprr[VERT_LEFT_PRED][12] = (pb + (pc<<1) + pd + 2) >>2;
    img->mprr[VERT_LEFT_PRED][6] = 
    img->mprr[VERT_LEFT_PRED][13] = (pc + (pd<<1) + pe + 2) >>2;
    img->mprr[VERT_LEFT_PRED][7] = 
    img->mprr[VERT_LEFT_PRED][14] = (pd + (pe<<1) + pf + 2) >>2;
    img->mprr[VERT_LEFT_PRED][15] = (pe + (pf<<1) + pg + 2) >>2;

     // Mode HOR_UP_PRED
    img->mprr[HOR_UP_PRED][0] = (pi + pj + 1) >>1;
    img->mprr[HOR_UP_PRED][1] = (pi + (pj<<1) + pk + 2) >>2;
    img->mprr[HOR_UP_PRED][2] = 
    img->mprr[HOR_UP_PRED][4] = (pj + pk + 1) >>1;
    img->mprr[HOR_UP_PRED][3] = 
    img->mprr[HOR_UP_PRED][5] = (pj + (pk<<1) + pl + 2) >>2;
    img->mprr[HOR_UP_PRED][6] = 
    img->mprr[HOR_UP_PRED][8] = (pk + pl + 1) >>1;
    img->mprr[HOR_UP_PRED][7] = 
    img->mprr[HOR_UP_PRED][9] = (pk + (pl<<1) + pl + 2) >>2;
    img->mprr[HOR_UP_PRED][12] = 
    img->mprr[HOR_UP_PRED][10] = 
    img->mprr[HOR_UP_PRED][11] = 
    img->mprr[HOR_UP_PRED][13] = 
    img->mprr[HOR_UP_PRED][14] = 
    img->mprr[HOR_UP_PRED][15] = pl;

    // Mode DIAG_DOWN_RIGHT_PRED
    img->mprr[DIAG_DOWN_RIGHT_PRED][12] = (pl + (pk<<1) + pj + 2) >>2; 
    img->mprr[DIAG_DOWN_RIGHT_PRED][8] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][13] = (pk + (pj<<1) + pi + 2) >>2; 
    img->mprr[DIAG_DOWN_RIGHT_PRED][4] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][9] = 
    img->mprr[DIAG_DOWN_RIGHT_PRED][14] = (pj + (pi<<1) + px + 2) >>2; 
    img->mprr[DIAG_DOWN_RIGHT_PRED][0] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][5] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][10] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][15] = (pi + (px<<1) + pa + 2) >>2; 
    img->mprr[DIAG_DOWN_RIGHT_PRED][1] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][6] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][11] = (px + (pa<<1) + pb + 2) >>2;
    img->mprr[DIAG_DOWN_RIGHT_PRED][2] =
    img->mprr[DIAG_DOWN_RIGHT_PRED][7] = (pa + (pb<<1) + pc + 2) >>2;
    img->mprr[DIAG_DOWN_RIGHT_PRED][3] = (pb + (pc<<1) + pd + 2) >>2;

     // Mode VERT_RIGHT_PRED
    img->mprr[VERT_RIGHT_PRED][0] = 
    img->mprr[VERT_RIGHT_PRED][9] = (px + pa + 1) >>1;
    img->mprr[VERT_RIGHT_PRED][1] = 
    img->mprr[VERT_RIGHT_PRED][10] = (pa + pb + 1) >>1;
    img->mprr[VERT_RIGHT_PRED][2] = 
    img->mprr[VERT_RIGHT_PRED][11] = (pb + pc + 1) >>1;
    img->mprr[VERT_RIGHT_PRED][3] = (pc + pd + 1) >>1;
    img->mprr[VERT_RIGHT_PRED][4] = 
    img->mprr[VERT_RIGHT_PRED][13] = (pi + (px<<1) + pa + 2) >>2;
    img->mprr[VERT_RIGHT_PRED][5] = 
    img->mprr[VERT_RIGHT_PRED][14] = (px + (pa<<1) + pb + 2) >>2;
    img->mprr[VERT_RIGHT_PRED][6] = 
    img->mprr[VERT_RIGHT_PRED][15] = (pa + (pb<<1) + pc + 2) >>2;
    img->mprr[VERT_RIGHT_PRED][7] = (pb + (pc<<1) + pd + 2) >>2;
    img->mprr[VERT_RIGHT_PRED][8] = (px + (pi<<1) + pj + 2) >>2;
    img->mprr[VERT_RIGHT_PRED][12] = (pi + (pj<<1) + pk + 2) >>2;

    // Mode HOR_DOWN_PRED
    img->mprr[HOR_DOWN_PRED][0] = 
    img->mprr[HOR_DOWN_PRED][6] = (px + pi + 1) >>1;
    img->mprr[HOR_DOWN_PRED][1] = 
    img->mprr[HOR_DOWN_PRED][7] = (pi + (px<<1) + pa + 2) >>2;
    img->mprr[HOR_DOWN_PRED][2] = (px + (pa<<1) + pb + 2) >>2;
    img->mprr[HOR_DOWN_PRED][3] = (pa + (pb<<1) + pc + 2) >>2;
    img->mprr[HOR_DOWN_PRED][4] = 
    img->mprr[HOR_DOWN_PRED][10] = (pi + pj + 1) >>1;
    img->mprr[HOR_DOWN_PRED][5] = 
    img->mprr[HOR_DOWN_PRED][11] = (px + (pi<<1) + pj + 2) >>2;
    img->mprr[HOR_DOWN_PRED][8] = 
    img->mprr[HOR_DOWN_PRED][14] = (pj + pk + 1) >>1;
    img->mprr[HOR_DOWN_PRED][9] = 
    img->mprr[HOR_DOWN_PRED][15] = (pi + (pj<<1) + pk + 2) >>2;
    img->mprr[HOR_DOWN_PRED][12] = (pk + pl + 1) >>1;
    img->mprr[HOR_DOWN_PRED][13] = (pj + (pk<<1) + pl + 2) >>2;
    
    best_intra_sad=MAX_VALUE; 		  
    upMode = img->ipredmode[pic_block_x+1][pic_block_y];
    leftMode = img->ipredmode[pic_block_x][pic_block_y+1];
    mostProbableMode  = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
    for (ipmode=0; ipmode < NO_INTRA_PMODE; ipmode++)
    {			  
	current_intra_sad  = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda_mode );			  
        current_intra_sad += Intra_Sad(img,imgY_org+img_offset,img->mprr[ipmode]);        
        if (current_intra_sad < best_intra_sad) 
        {
           best_intra_sad=current_intra_sad;
           best_ipmode=ipmode;
        }      
    }
    *tot_intra_sad += best_intra_sad;
    img->ipredmode[pic_block_x+1][pic_block_y+1]=best_ipmode;
    img->intra_pred_modes[block_num] = mostProbableMode == best_ipmode ? -1 : best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1;
		                            
    nonzero = dct_luma(imgY_org+img_offset,img->mprr[best_ipmode],block_x,block_y,img);     
  return nonzero;
}

/*yummy*/
int intrapred_lumaMB_other(struct img_par *img,int img_x,int img_y,int *tot_intra_sad,int *imode)
{
  int i,j,s0;  
  int s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16;
  int ss1,ss2,ss3,ss4,ss5,ss6,ss7,ss8,ss9,ss10,ss11,ss12,ss13,ss14,ss15,ss16;
  int width=img->width+IMG_PAD_SIZE;
  int img_offset = img_y*width+img_x;
  int temp = img_offset-width;
  int current_intra_sad;
  int ih,iv;
  int ib,ic,iaa;

  /*up pixel*/
  s1 = imgY[temp];
  s2 = imgY[temp+1];
  s3 = imgY[temp+2];
  s4 = imgY[temp+3];
  s5 = imgY[temp+4];
  s6 = imgY[temp+5];
  s7 = imgY[temp+6];
  s8 = imgY[temp+7];
  s9 = imgY[temp+8];
  s10 = imgY[temp+9];
  s11 = imgY[temp+10];
  s12 = imgY[temp+11];
  s13 = imgY[temp+12];
  s14 = imgY[temp+13];
  s15 = imgY[temp+14];
  s16 = imgY[temp+15];

  /*left pixel*/
  temp +=width-1;
  ss1  = imgY[temp]; temp +=width;
  ss2  = imgY[temp]; temp +=width;
  ss3  = imgY[temp]; temp +=width;
  ss4  = imgY[temp]; temp +=width;
  ss5  = imgY[temp]; temp +=width;
  ss6  = imgY[temp]; temp +=width;
  ss7  = imgY[temp]; temp +=width;
  ss8  = imgY[temp]; temp +=width;
  ss9  = imgY[temp]; temp +=width;
  ss10 = imgY[temp]; temp +=width;
  ss11 = imgY[temp]; temp +=width;
  ss12 = imgY[temp]; temp +=width;
  ss13 = imgY[temp]; temp +=width;
  ss14 = imgY[temp]; temp +=width;
  ss15 = imgY[temp]; temp +=width;
  ss16 = imgY[temp];

  ///////////////////////////////
  // make horiz and vert prediction
  ///////////////////////////////

  for (i=0; i < 256; i+=16)
  {
    img->mprr_2[VERT_PRED][i]	  = s1;
    img->mprr_2[VERT_PRED][1+i]  = s2;
    img->mprr_2[VERT_PRED][2+i]  = s3;
    img->mprr_2[VERT_PRED][3+i]  = s4;
	img->mprr_2[VERT_PRED][4+i]  = s5;
    img->mprr_2[VERT_PRED][5+i]  = s6;
    img->mprr_2[VERT_PRED][6+i]  = s7;
    img->mprr_2[VERT_PRED][7+i] = s8;
	img->mprr_2[VERT_PRED][8+i] = s9;
    img->mprr_2[VERT_PRED][9+i] = s10;
    img->mprr_2[VERT_PRED][10+i] = s11;
    img->mprr_2[VERT_PRED][11+i] = s12;
	img->mprr_2[VERT_PRED][12+i] = s13;
    img->mprr_2[VERT_PRED][13+i] = s14;
    img->mprr_2[VERT_PRED][14+i] = s15;
    img->mprr_2[VERT_PRED][15+i] = s16;
  } 
  *tot_intra_sad = IntraMB_Sad(img,imgY_org+img_offset,img->mprr_2[VERT_PRED]);
  *imode = VERT_PRED_16;
  

  for (i=0; i < 16; i++)
  {
    img->mprr_2[HOR_PRED][i]	 = ss1;
    img->mprr_2[HOR_PRED][16+i]  = ss2;
    img->mprr_2[HOR_PRED][32+i]  = ss3;
    img->mprr_2[HOR_PRED][48+i]  = ss4;
	img->mprr_2[HOR_PRED][64+i]	 = ss5;
    img->mprr_2[HOR_PRED][80+i]  = ss6;
    img->mprr_2[HOR_PRED][96+i]  = ss7;
    img->mprr_2[HOR_PRED][112+i] = ss8;
	img->mprr_2[HOR_PRED][128+i] = ss9;
    img->mprr_2[HOR_PRED][144+i] = ss10;
    img->mprr_2[HOR_PRED][160+i] = ss11;
    img->mprr_2[HOR_PRED][176+i] = ss12;
	img->mprr_2[HOR_PRED][192+i] = ss13;
    img->mprr_2[HOR_PRED][208+i] = ss14;
    img->mprr_2[HOR_PRED][224+i] = ss15;
    img->mprr_2[HOR_PRED][240+i] = ss16;
  }  
  current_intra_sad = IntraMB_Sad(img,imgY_org+img_offset,img->mprr_2[HOR_PRED]);
  if( current_intra_sad < (*tot_intra_sad))
  {
	  *tot_intra_sad = current_intra_sad;
	  *imode = HOR_PRED_16;
  }

  ///////////////////////////////
  // make DC prediction
  ///////////////////////////////
    // left edge
  s0 = (ss1 +ss2 +ss3 +ss4 +ss5 +ss6 +ss7 +ss8 +ss9 +ss10 +ss11 +ss12 +ss13 +ss14 +ss15 +ss16 
	  +s1 +s2 +s3 +s4 +s5 +s6 +s7 +s8 +s9 +s10 +s11 +s12 +s13 +s14 +s15 +s16+16)>>5;                
  for (j=0; j < 256; j+=16)
  {
      img->mprr_2[DC_PRED][j]	= s0;		img->mprr_2[DC_PRED][j+1] = s0;
      img->mprr_2[DC_PRED][j+2] = s0;		img->mprr_2[DC_PRED][j+3] = s0;
	  img->mprr_2[DC_PRED][j+4] = s0;		img->mprr_2[DC_PRED][j+5] = s0;
      img->mprr_2[DC_PRED][j+6] = s0;		img->mprr_2[DC_PRED][j+7] = s0;
	  img->mprr_2[DC_PRED][j+8] = s0;		img->mprr_2[DC_PRED][j+9] = s0;
      img->mprr_2[DC_PRED][j+10] = s0;		img->mprr_2[DC_PRED][j+11] = s0;
	  img->mprr_2[DC_PRED][j+12] = s0;		img->mprr_2[DC_PRED][j+13] = s0;
      img->mprr_2[DC_PRED][j+14] = s0;		img->mprr_2[DC_PRED][j+15] = s0;
  }
  current_intra_sad = IntraMB_Sad(img,imgY_org+img_offset,img->mprr_2[DC_PRED]);
  if( current_intra_sad < (*tot_intra_sad))
  {
	  *tot_intra_sad = current_intra_sad;
	  *imode = DC_PRED_16;
  }

  /*plane predict*/
  ih=0;
  iv=0;
  temp=(img_y-1)*width+img_x;
  for (i=1;i<9;i++)
  {
    if (i<8)
      ih += i*(imgY[temp+7+i] - imgY[temp+7-i]);
    else
      ih += i*(imgY[temp+7+i] - imgY[temp-1]);
    
    iv += i*(imgY[(img_y+7+i)*width+img_x-1] - imgY[(img_y+7-i)*width+img_x-1]);
  }
  ib=(5*ih+32)>>6;
  ic=(5*iv+32)>>6;
  
  iaa=16*(imgY[(img_y-1)*width+img_x+15]+imgY[(img_y+15)*width+img_x-1]);

  for (j=0;j< MB_BLOCK_SIZE;j++)
  {
    for (i=0;i< MB_BLOCK_SIZE;i++)
    {
      img->mprr_2[PLANE_16][j*16+i]=
		  max(0,min(255,(iaa+(i-7)*ib +(j-7)*ic + 16)>>5));// store plane prediction
    }
  }
  current_intra_sad = IntraMB_Sad(img,imgY_org+img_offset,img->mprr_2[PLANE_16]);
  if( current_intra_sad < (*tot_intra_sad))
  {
	  *tot_intra_sad = current_intra_sad;
	  *imode = PLANE_16;
  }
  return *tot_intra_sad; 
}

int intrapred_luma_other(struct img_par *img,int img_x,int img_y,int *tot_intra_sad,int block_num)
{
  int i,j,s0;
  int pa,pb,pc,pd,pe,pf,pg,ph,pi,pj,pk,pl,px;
  int pic_block_x=img_x>>2;
  int pic_block_y=img_y>>2;
  int block_x=img_x&15;
  int block_y=img_y&15;
  int width=img->width+IMG_PAD_SIZE;
  int best_intra_sad;
  int upMode,leftMode,mostProbableMode,best_ipmode,ipmode;
  double lambda_mode = QP2QUANT[max(0,img->qp-SHIFT_QP)];
  int nonzero;
  int img_offset = img_y*width+img_x;
  int temp = img_offset-width;
  int current_intra_sad;

  	  pa=imgY[temp];
	  pb=imgY[temp+1];
	  pc=imgY[temp+2];
	  pd=imgY[temp+3];

	  pe=imgY[temp+4];
	  pf=imgY[temp+5];
	  pg=imgY[temp+6];
	  ph=imgY[temp+7];

	  px=imgY[temp-1]; temp +=width;
	  pi=imgY[temp-1]; temp +=width;
	  pj=imgY[temp-1]; temp +=width;
	  pk=imgY[temp-1]; temp +=width;
	  pl=imgY[temp-1];
  
  ///////////////////////////////
  // make DC prediction
  ///////////////////////////////
    s0 = (pa + pb + pc + pd + pi + pj + pk + pl + 4)>>3;

  for (j=0; j < BLOCK_SIZE; j++)
  {
  	i=j*4;
      img->mprr[DC_PRED][i] = s0;
      img->mprr[DC_PRED][i+1] = s0;
      img->mprr[DC_PRED][i+2] = s0;
      img->mprr[DC_PRED][i+3] = s0;
  }

  ///////////////////////////////
  // make horiz and vert prediction
  ///////////////////////////////

  for (i=0; i < BLOCK_SIZE; i++)
  {
    img->mprr[VERT_PRED][i*4] = pa;
    img->mprr[VERT_PRED][i*4+1] = pb;
    img->mprr[VERT_PRED][i*4+2] = pc;
    img->mprr[VERT_PRED][i*4+3] = pd;
    img->mprr[HOR_PRED][i]  = pi;
    img->mprr[HOR_PRED][4+i]  = pj;
    img->mprr[HOR_PRED][8+i]  = pk;
    img->mprr[HOR_PRED][12+i]  = pl;
  }

    // Mode DIAG_DOWN_LEFT_PRED
    img->mprr[DIAG_DOWN_LEFT_PRED][0] = (pa + pc + (pb<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][1] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][4] = (pb + pd + (pc<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][2] =
    img->mprr[DIAG_DOWN_LEFT_PRED][5] =
    img->mprr[DIAG_DOWN_LEFT_PRED][8] = (pc + pe + (pd<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][3] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][6] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][9] = 
    img->mprr[DIAG_DOWN_LEFT_PRED][12] = (pd + pf + (pe<<1) + 2) >>2;
    img->mprr[DIAG_DOWN_LEFT_PRED][7] = 

⌨️ 快捷键说明

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