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

📄 image.c

📁 avs源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
	int i,j;
	
  img->fld_type = 0;
	
	
  imgY_org  = imgY_org_top;
  imgUV_org = imgUV_org_top;
	 
	//intial ref index for field coding 
  for(i=0;i<4;i++)              // 3 2 1 0
		ref[i] = ref_fld[i]; 
	
	//initialize ref index 1/4 pixel
	for(i=0;i<4;i++)
	{
		mref[i] = mref_fld[i];
	}
	
	//integer chroma pixel for interlace 
	for (j=0;j<4;j++)//ref_index = 0
		for (i=0;i<2;i++)
		{
			mcef[j][i] = ref_fld[j][i+1];
		}
		
		//integer luma pixel for interlace
		for(i=0;i<4;i++)
		{
			Refbuf11[i] = &ref[i][0][0][0];
		}
		
		imgY  = imgY_top   = current_field[0];
		imgUV = imgUV_top  = &current_field[1];
		
		tmp_mv = tmp_mv_top;
		refFrArr = refFrArr_top;
		fw_refFrArr = fw_refFrArr_top;
		bw_refFrArr = bw_refFrArr_top;
}
  
/*
*************************************************************************
* Function:point to bottom field coding variables 
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void put_buffer_bot()
{
	int i,j;

  img->fld_type = 1; 
  
  imgY_org = imgY_org_bot;
  imgUV_org = imgUV_org_bot;
  
  tmp_mv = tmp_mv_bot;
  refFrArr = refFrArr_bot;
  fw_refFrArr = fw_refFrArr_bot;
  bw_refFrArr = bw_refFrArr_bot;
  
   //intial ref index for field coding 
  for(i=0;i<4;i++)
  	ref[i] = ref_fld[i]; 

	//initialize ref index 1/4 pixel
	for(i=0;i<4;i++)
	{
		mref[i] = mref_fld[i];
	}

	//integer chroma pixel for interlace 
	for (j=0;j<4;j++)//ref_index = 0
	for (i=0;i<2;i++)
	{
		mcef[j][i] = ref_fld[j][i+1];
	}
	
	//integer luma pixel for interlace
	for(i=0;i<4;i++)
	{
		Refbuf11[i] = &ref[i][0][0][0];
	}
	//imgY = imgY_bot;
    //imgUV = imgUV_bot;
	imgY_bot = current_field[0];
    imgUV_bot = &current_field[1];
	imgY = imgY_bot;
    imgUV = imgUV_bot;
}
  
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void split_field_top()
{
  int i;

  imgY = imgY_top;
	imgUV = imgUV_top;
  
	for (i=0; i<img->height; i++)
  {
    memcpy(imgY[i], imgY_frm[i*2], img->width); 
  }
  
  for (i=0; i<img->height_cr; i++)
  {
    memcpy(imgUV[0][i], imgUV_frm[0][i*2], img->width_cr);
    memcpy(imgUV[1][i], imgUV_frm[1][i*2], img->width_cr);
  }

	UnifiedOneForthPix (imgY, imgUV[0], imgUV[1],
    mref[1]);
}

  
/*
*************************************************************************
* Function:extract bottom field from a frame 
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void split_field_bot()
{
  int i;

	imgY = imgY_bot;
	imgUV = imgUV_bot;
    
  for (i=0; i<img->height; i++)
  {
    memcpy(imgY[i], imgY_frm[i*2 + 1], img->width);
  }
  
  for (i=0; i<img->height_cr; i++)
  {
    memcpy(imgUV[0][i], imgUV_frm[0][i*2 + 1], img->width_cr);
    memcpy(imgUV[1][i], imgUV_frm[1][i*2 + 1], img->width_cr);
  }
  
		UnifiedOneForthPix (imgY, imgUV[0], imgUV[1],
    mref[0]);
}

/*
*************************************************************************
* Function:update the decoder picture buffer
* Input:frame number in the bitstream and the video sequence
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void Update_Picture_Buffers()
{ 
	unsigned char ***tmp;
	unsigned char **tmp_y;
	int i;
		
	//update integer pixel reference buffer
	tmp = ref_frm[1];       //ref_frm[ref_index][yuv][height][width] ref_index = 0,1  for P frame
  ref_frm[1] = ref_frm[0];    // ref_index = 0, backward reference for B frame; 1: forward reference for B frame
  ref_frm[0] = current_frame; // current_frame: current image under reconstruction
  current_frame = tmp;	
  
  //update luma 1/4 pixel reference buffer mref[ref_index][height][width] ref_index = 0,1 for P frame
	tmp_y = mref_frm[1];              // ref_index = 0, forward refernce for B frame ; 1: backward refernce for B frame
	mref_frm[1] = mref_frm[0];
	mref_frm[0] = tmp_y;
	
	//initial reference index, and for coming interpolation in mref[0]
	for(i=0;i<2;i++)
	{
		mref[i] = mref_frm[i];
	}
}



void Update_Picture_Buffers_top_field()
{ 
	unsigned char ***tmp;
	unsigned char **tmp_y;
	int i,j;
		

		//update integer pixel reference frame buffer 
		tmp = ref_fld[4];
		for (i=4; i>0; i--)
			ref_fld[i] = ref_fld[i-1];
		ref_fld[0] = current_field;
		current_field = tmp;
		
		//update 
		tmp_y = mref_fld[3];
		for (j=3;j>0;j--)//ref_index = 0
		{
			mref_fld[j] = mref_fld[j-1];
		}
    mref_fld[0] = tmp_y;
		
		//for interpolation
		//for(i=0;i<3;i++)
    for(i=0;i<4;i++)
		{
			mref[i] = mref_fld[i];
		} 
}

void Update_Picture_Buffers_bot_field()
{ 
	unsigned char ***tmp;
	unsigned char **tmp_y;
	int i,j;
		
	
		//update integer pixel reference frame buffer 
		tmp = ref_fld[4];
		for (i=4; i>0; i--)
			ref_fld[i] = ref_fld[i-1];
		ref_fld[0] = current_field;
		current_field = tmp;
		
		//update 1/4 pixel reference for interpolation
		tmp_y = mref_fld[3];
		for (j=3;j>0;j--)//ref_index = 0
		{
			mref_fld[j] = mref_fld[j-1];
		}
    mref_fld[0] = tmp_y;
		
		//for interpolation
		//for(i=0;i<3;i++)
    for(i=0;i<4;i++)
		{
			mref[i] = mref_fld[i];
		} 
}

int DetectLumVar()
{
	int i , j ;
	int Histogtam_Cur[256] ;
	int Histogtam_Pre[256] ;
	int temp = 0 ;

	for( i = 0 ; i < 256 ; i++){
		Histogtam_Cur[i] = 0 ; 
		Histogtam_Pre[i] = 0 ;
	}
	
	for(j = 0 ; j < img->height ; j++){
		for( i = 0 ; i < img->width ; i++){
			Histogtam_Cur[imgY_org[j][i]] += 1 ;
			Histogtam_Pre[Refbuf11[0][j*img->width + i]] += 1 ;
		}
	}

	for(i = 0 ; i < 256 ; i++){
		temp += abs(Histogtam_Pre[i] - Histogtam_Cur[i]);
	}
   
	// if(temp >= ((img->height*img->width)*2)){
	if(temp >= ((img->height*img->width)/4)){
		return 1;
	}
	else
	{
		return 0;
	}
}

void CalculateBrightnessPar(int currentblock[16][16] , int preblock[16][16] , float *c , float *d)
{
	int N = 256 ;
	int i , j ;
	int m1,m2,m3,m4,m5,m6;
	
	m1 = m2 = m3 = m4 = m5 = m6 = 0 ;
	for(j = 0 ; j < 16 ; j++){
		for(i = 0 ; i < 16 ; i++){
			m1 += preblock[j][i]*preblock[j][i] ;
			m2 += preblock[j][i];
			m3 += preblock[j][i];
			m4 += 1;
			m5 += preblock[j][i]*currentblock[j][i] ;
			m6 += currentblock[j][i]; 
		}
	}
	*c = ((float)(m4*m5 - m2*m6)) / ((float)(m1*m4 - m2*m3));
	*d = ((float)(m3*m5 - m6*m1)) / ((float)(m3*m2 - m1*m4));
	return ;
}

void CalculatePar(int refnum)
{
	int mbx , mby ;
	int currmb[16][16] ;
	int refmb[16][16] ;
	float alpha ;
	float belta ;
	int i , j ;
	int Alpha_His[256];
	int Belta_His[256];
	int max_num = 0 ;
	int max_index = -1 ;
	int belta_sum = 0 ;

	for( i = 0 ; i < 256 ; i++){
		Alpha_His[i] = 0 ;
		Belta_His[i] = 0 ;
	}

	for(mby = 0 ; mby < img->height/16 ; mby++){
		for(mbx = 0 ; mbx < img->width/16 ; mbx++){
			for( j = 0 ; j < 16 ; j++){
				for( i = 0 ; i < 16 ; i++){
					currmb[j][i] = imgY_org[mby*16+j][mbx*16+i];
					refmb [j][i] = Refbuf11[refnum][(mby*16+j)*img->width + mbx*16+i] ;
				}
			}
			CalculateBrightnessPar(currmb,refmb,&alpha,&belta);
			allalpha_lum[mby*(img->width/16)+mbx] = (int)(alpha*32);
			allbelta_lum[mby*(img->width/16)+mbx] = (int)(belta);
		}
	}

	for(i = 0 ; i < ((img->height/16)*(img->width/16)) ; i++)
	{
		// !!  [12/28/2005] cjw  shenyanfei 
		if((0 < allalpha_lum[i]) &&( allalpha_lum[i] < 256)&&(abs(allbelta_lum[i]) < 127))
		{
			Alpha_His[abs(allalpha_lum[i])]++;
		}
	}

	for( i = 4 ; i < 256 ; i++) // !! 4-256 shenyanfei 
	{
		if(Alpha_His[i] > max_num)
		{
			max_num = Alpha_His[i] ;
			max_index = i ;
		}
	}

	for( i = 0 ; i < ((img->height/16)*(img->width/16)) ; i++){
		if(allalpha_lum[i] == max_index){
			belta_sum += allbelta_lum[i] ;
		}
	}
	img->lum_scale[refnum] = max_index ;
		
	if (max_num == 0) {
				max_num = max_num ;
		}
	
	img->lum_shift[refnum] = belta_sum/max_num ;

	//cjw 20060327 for shift range limit  7.2.4
	img->lum_shift[refnum]= Clip3(-128,127,img->lum_shift[refnum]);

	if(max_num > ((img->height/16)*(img->width/16) / 2))
		img->mb_weighting_flag = 0 ; //all the NoIntra mbs are WP 
	else
		img->mb_weighting_flag = 1 ; 

	img->chroma_scale[refnum] = img->lum_scale[refnum] ;  // cjw default chroma value same with luma
	img->chroma_shift[refnum] = img->lum_shift[refnum]  ; // cjw default chroma value same with luma

//	img->lum_scale[refnum] = 28;	 //cjw just for debug
//	img->lum_shift[refnum] = 0;		 //cjw just for debug
//	img->chroma_scale[refnum] = 29 ; // cjw just for debug
//	img->chroma_shift[refnum] = 0  ; // cjw just for debug
	return ;
}

void estimate_weighting_factor()
{
	int   bframe    = (img->type==B_IMG);
	int   max_ref   = img->nb_references;
	int   ref_num ;

	if(max_ref > img->buf_cycle)
		max_ref = img->buf_cycle;

	// !! detection luminance variation
	img->LumVarFlag = DetectLumVar();
	img->LumVarFlag = 1; //cjw 20051230 just for debug

	if(img->LumVarFlag == 1){
		for(ref_num = 0 ; ref_num < max_ref ; ref_num++){
			CalculatePar(ref_num);
		}
	}
	return;
}

//then for field coding the parameters are achieved from frame coding function estimate_weighting_factor()
void estimate_weighting_factor_field()
{
	int   bframe    = (img->type==B_IMG);
	int   max_ref   = img->nb_references;
	int   ref_num ;

	if(max_ref > img->buf_cycle)
		max_ref = img->buf_cycle;

	// !! detection luminance variation
	img->LumVarFlag = DetectLumVar();  
	img->LumVarFlag = 1; //cjw 20051230 just for debug

	if (img->LumVarFlag == 1) {
		for(ref_num = 0 ; ref_num < max_ref ; ref_num++){
			CalculatePar(ref_num);
		}
	}
	return;
}

⌨️ 快捷键说明

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