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

📄 macroblock.c

📁 avs-s最新代码,包括编码器和解码器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
		int mv[2];
		refframe = fw_ref;
		delta_P = 2*(img->imgtr_next_P_frm - img->imgtr_last_P_frm);
		delta_P = (delta_P + 512)%512;   // Added by Xiaozhen ZHENG, 2007.05.05
		if(img->picture_structure)
			TRp = (refframe+1)*delta_P;  //the lates backward reference
		else
		{
			TRp = delta_P;//refframe == 0 ? delta_P-1 : delta_P+1;
		}
		delta_PB = 2*(picture_distance - img->imgtr_last_P_frm);     // Tsinghua  200701
		TRp  = (TRp + 512)%512;
		delta_PB = (delta_PB + 512)%512;  // Added by Xiaozhen ZHENG, 2007.05.05
		if(!img->picture_structure)
		{
			if(img->current_mb_nr_fld < img->total_number_mb) //top field
				DistanceIndexFw =  refframe == 0 ? delta_PB-1:delta_PB;
			else
				DistanceIndexFw =  refframe == 0 ? delta_PB:delta_PB+1;
		}
		else
			DistanceIndexFw = delta_PB;	    

		//DistanceIndexBw    = TRp - DistanceIndexFw;      
		DistanceIndexBw    = (TRp - DistanceIndexFw+512)%512; // Added by Zhijie Yang, 20070419, Broadcom

		mv[0] = - ((fmv_array[bx][by][fw_ref][fw_mode][0]*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9);
		mv[1] = - ((fmv_array[bx][by][fw_ref][fw_mode][1]*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9);

#ifdef half_pixel_compensation
		if(!img->picture_structure)
		{
			int delt, delt2;
			MV_compensation_sym(&delt, &delt2, 1-refframe, refframe); //for symmetric mode
			assert(delt2 == delt);
			mv[1] = - (((fmv_array[bx][by][fw_ref][fw_mode][1]+delt)*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9)-delt2;
		}
#endif

		if(fw_mode && bw_mode)
			OneComponentLumaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, mv,
			(!img->picture_structure) ? (-2+bw_ref) : (-1-bw_ref));
		else
			OneComponentLumaPrediction4x4 (bw_pred, pic_pix_x, pic_pix_y, bmv_array[bx][by][bw_ref][bw_mode],
			(!img->picture_structure) ? (-2+bw_ref) : (-1-bw_ref));
	}

	// !! start shenyanfei
	if (direct || (fw_mode && bw_mode))
	{	
		if(!img->picture_structure)
			bw_ref=1-bw_ref;   //cjw used for bw reference buffer address changing 20060112 bw

		fw_ref_num=(img->picture_structure)?(fw_ref):(2*fw_ref);  //cjw 20060112 fw 
		bw_ref_num=(img->picture_structure)?(bw_ref+1):(2*bw_ref+1);  //cjw 20060112 bw

		fw_lum_scale = img->lum_scale[fw_ref_num];
		fw_lum_shift = img->lum_shift[fw_ref_num];
		bw_lum_scale = img->lum_scale[bw_ref_num];
		bw_lum_shift = img->lum_shift[bw_ref_num];

		for(j=block_y; j<block_y4; j++)
			for (i=block_x; i<block_x4; i++){
				img->mpr[i][j] = (*fpred + *bpred + 1) / 2;

				if(img->LumVarFlag == 1)
					img->mpr_weight[i][j] = (Clip1((((*fpred)*fw_lum_scale + 16)>>5) + fw_lum_shift)+
					Clip1((((*bpred)*bw_lum_scale + 16)>>5) + bw_lum_shift) + 1)/2;

				fpred++;
				bpred++;
			}
	}
	else if (fw_mode || skipped)  //P fw and B one direction fw 
	{
		if(img->type==B_IMG){
			fw_ref_num=(img->picture_structure)?(fw_ref):(2*fw_ref);  //cjw 20060112 fw 

			fw_lum_scale = img->lum_scale[fw_ref_num];  //cjw 20060112
			fw_lum_shift = img->lum_shift[fw_ref_num];
		}
		else{
			fw_lum_scale = img->lum_scale[fw_ref];
			fw_lum_shift = img->lum_shift[fw_ref];
		}

		for(j=block_y; j<block_y4; j++)
			for (i=block_x; i<block_x4; i++){
				img->mpr[i][j] = *fpred;
				if((!skipped) && (img->LumVarFlag == 1)){
					img->mpr_weight[i][j] = Clip1(((((*fpred)*fw_lum_scale + 16)>>5) + fw_lum_shift));
				}
				fpred++;
			}
	}
	else  //B one direction bw
	{
		if(!img->picture_structure)//cjw used for bw reference buffer address changing 20060112 bw
			bw_ref=1-bw_ref;   

		bw_ref_num=(img->picture_structure)?(bw_ref+1):(2*bw_ref+1);  //cjw 20060112 bw

		bw_lum_scale = img->lum_scale[bw_ref_num];
		bw_lum_shift = img->lum_shift[bw_ref_num];
		for(j=block_y; j<block_y4; j++)
			for (i=block_x; i<block_x4; i++){
				img->mpr[i][j] = *bpred;

				if(img->LumVarFlag == 1){
					img->mpr_weight[i][j] = Clip1(((((*bpred)*bw_lum_scale + 16)>>5) + bw_lum_shift));
				}
				bpred++;
			}
	}
	// !! end shenyanfei

}

/*
*************************************************************************
* Function:Residual Coding of an 8x8 Luma block (not for intra)
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

int                                       //  ==> coefficient cost
LumaResidualCoding8x8 (int  *cbp,         //  --> cbp (updated according to processed 8x8 luminance block)
					   int  *cbp_blk,     //  --> block cbp (updated according to processed 8x8 luminance block)
					   int  block8x8,     // <--  block number of 8x8 block
					   int  fw_mode,      // <--  forward  prediction mode (1-7, 0=DIRECT)
					   int  bw_mode,      // <--  backward prediction mode (1-7, 0=DIRECT)
					   int  fw_refframe,  // <--  reference frame for forward prediction
					   int  bw_refframe   // <--  reference frame for backward prediction
					   )
{
	int    block_y, block_x, pic_pix_y, pic_pix_x, i, j, cbp_blk_mask;
	int    coeff_cost = 0;
	int    mb_y       = (block8x8 / 2) << 3;
	int    mb_x       = (block8x8 % 2) << 3;
	int    cbp_mask   = 1 << block8x8;
	int    bxx, byy;                   // indexing curr_blk
	int    scrFlag = 0;                // 0=noSCR, 1=strongSCR, 2=jmSCR
	byte** imgY_original = imgY_org;
	int  pix_x    = img->pix_x;
	int  pix_y    = img->pix_y;
	Macroblock* currMB = &img->mb_data[img->current_mb_nr];
	int    direct    = (fw_mode == 0 && bw_mode == 0 && (img->type==B_IMG));
	int    skipped   = (fw_mode == 0 && bw_mode == 0 && (img->type!=B_IMG));
	short    curr_blk[B8_SIZE][B8_SIZE]; // AVS 8x8 pred.error buffer
	int  incr_y=1,off_y=0; /*lgp*/

	int IntraPrediction=IS_INTRA (currMB);  //cjw 20060321


	if (img->type==B_IMG)
		scrFlag = 1;


	//===== loop over 4x4 blocks =====
	for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
	{
		//pic_pix_y = pix_y + block_y;/*lgp*/
		pic_pix_y = pix_y + mb_y;/*lgp*/

		for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
		{
			pic_pix_x = pix_x + block_x;

			cbp_blk_mask = (block_x>>2) + block_y;

			//===== prediction of 4x4 block =====
			LumaPrediction4x4 (block_x, block_y, fw_mode, bw_mode, fw_refframe, bw_refframe);


			// !! start shenyanfei 
			//if(((!direct)&&(!skipped)&&(img->LumVarFlag == 1) && (img->mb_weighting_flag == 0)&&(img->weighting_prediction == 1))
			//if(((!skipped)&&(!direct)&&(img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)&&(img->weighting_prediction == 1))  //cjw 20051230
			if(((!skipped) && (img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)&&(img->weighting_prediction == 1)) //cjw 20060321
				||((img->LumVarFlag == 1) && (img->mb_weighting_flag == 0))){
					for (j=0; j<4; j++)
						for (i=0; i<4; i++)
							img->mpr[i+block_x][j+block_y] = img->mpr_weight[i+block_x][j+block_y];
				}
				// !! end shenyanfei

				//===== get displaced frame difference ======
				for (j=0; j<4; j++)
					for (i=0; i<4; i++)
					{
						img->m7[i][j] = curr_blk[byy+j][bxx+i] = 
							imgY_original[pic_pix_y+incr_y*(j+byy)+off_y/*lgp*/][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
					}
		}
	}

	if (!skipped)
	{
		transform_B8(curr_blk);
		coeff_cost = scanquant_B8   (img->qp-MIN_QP, 0, block8x8, curr_blk, scrFlag, cbp, cbp_blk);	
	}
	/*
	The purpose of the action below is to prevent that single or 'expensive' coefficients are coded.
	With 4x4 transform there is larger chance that a single coefficient in a 8x8 or 16x16 block may be nonzero.
	A single small (level=1) coefficient in a 8x8 block will cost: 3 or more bits for the coefficient,
	4 bits for EOBs for the 4x4 blocks,possibly also more bits for CBP.  Hence the total 'cost' of that single
	coefficient will typically be 10-12 bits which in a RD consideration is too much to justify the distortion improvement.
	The action below is to watch such 'single' coefficients and set the reconstructed block equal to the prediction according
	to a given criterium.  The action is taken only for inter luma blocks.

	Notice that this is a pure encoder issue and hence does not have any implication on the standard.
	coeff_cost is a parameter set in dct_luma() and accumulated for each 8x8 block.  If level=1 for a coefficient,
	coeff_cost is increased by a number depending on RUN for that coefficient.The numbers are (see also dct_luma()): 3,2,2,1,1,1,0,0,...
	when RUN equals 0,1,2,3,4,5,6, etc.
	If level >1 coeff_cost is increased by 9 (or any number above 3). The threshold is set to 3. This means for example:
	1: If there is one coefficient with (RUN,level)=(0,1) in a 8x8 block this coefficient is discarded.
	2: If there are two coefficients with (RUN,level)=(1,1) and (4,1) the coefficients are also discarded
	sum_cnt_nonz is the accumulation of coeff_cost over a whole macro block.  If sum_cnt_nonz is 5 or less for the whole MB,
	all nonzero coefficients are discarded for the MB and the reconstructed block is set equal to the prediction.
	*///Lou

	/*lgp*dct*/

	if (!skipped && coeff_cost <= _LUMA_COEFF_COST_)
	{
		coeff_cost  = 0;
		(*cbp)     &=  (63 - cbp_mask);
		(*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8%2)));

		for (i=mb_x; i<mb_x+8; i++)
			for (j=mb_y; j<mb_y+8; j++)
			{
				imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
			}
	}

	return coeff_cost;
}


void                                       //  ==> coefficient cost
LumaPrediction (int  *cbp,         //  --> cbp (updated according to processed 8x8 luminance block)
				int  *cbp_blk,     //  --> block cbp (updated according to processed 8x8 luminance block)
				int  block8x8,     // <--  block number of 8x8 block
				int  fw_mode,      // <--  forward  prediction mode (1-7, 0=DIRECT)
				int  bw_mode,      // <--  backward prediction mode (1-7, 0=DIRECT)
				int  fw_refframe,  // <--  reference frame for forward prediction
				int  bw_refframe   // <--  reference frame for backward prediction
				)
{
	int    block_y, block_x, pic_pix_y, pic_pix_x, cbp_blk_mask;
	int    coeff_cost = 0;
	int    mb_y       = (block8x8 / 2) << 3;
	int    mb_x       = (block8x8 % 2) << 3;
	int    cbp_mask   = 1 << block8x8;
	int    bxx, byy;                   // indexing curr_blk
	int    scrFlag = 0;                // 0=noSCR, 1=strongSCR, 2=jmSCR
	byte** imgY_original = imgY_org;
	int  pix_x    = img->pix_x;
	int  pix_y    = img->pix_y;
	Macroblock* currMB = &img->mb_data[img->current_mb_nr];
	int    skipped    = (fw_mode == 0 && bw_mode == 0 && (img->type!=B_IMG));
	int  incr_y=1,off_y=0; /*lgp*/

	if (img->type==B_IMG)
		scrFlag = 1;

	//===== loop over 4x4 blocks =====
	for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
	{

		pic_pix_y = pix_y + mb_y;/*lgp*/

		for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
		{
			pic_pix_x = pix_x + block_x;

			cbp_blk_mask = (block_x>>2) + block_y;
			//===== prediction of 4x4 block =====
			LumaPrediction4x4 (block_x, block_y, fw_mode, bw_mode, fw_refframe, bw_refframe);
		}
	}

	return ;
}

/*
*************************************************************************
* Function:Set mode parameters and reference frames for an 8x8 block
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void
SetModesAndRefframe (int b8, int* fw_mode, int* bw_mode, int* fw_ref, int* bw_ref)
{
	Macroblock* currMB = &img->mb_data[img->current_mb_nr];
	int         j      = (b8/2);
	int         i      = (b8%2);
	int**     frefarr = refFrArr;   // For MB level field/frame coding
	int**     fw_refarr = fw_refFrArr;  // For MB level field/frame coding
	int**     bw_refarr = bw_refFrArr;  // For MB level field/frame coding
	int     block_x = img->block_x; 
	int     block_y = img->block_y; // For MB level field/frame coding

	*fw_mode = *bw_mode = *fw_ref = *bw_ref = -1;

	if (img->type!=B_IMG)
	{
		*fw_ref = frefarr[(block_y>>1)+j][(block_x>>1)+i];
		*bw_ref = 0;
		*bw_mode  = 0;
		*fw_mode  = currMB->b8mode[b8];
	}
	else
	{
		if (currMB->b8pdir[b8]==-1)
		{
			*fw_ref   = -1;
			*bw_ref   = -1;
			*fw_mode  =  0;
			*bw_mode  =  0;
		}
		else if (currMB->b8pdir[b8]==0)
		{
			*fw_ref   = fw_refarr[(block_y>>1)+j][(block_x>>1)+i];
			*bw_ref   = 0;
			*fw_mode  = currMB->b8mode[b8];
			*bw_mode  = 0;
		}
		else if (currMB->b8pdir[b8]==1)
		{
			*fw_ref   = 0;
			*bw_ref   = bw_refarr[(block_y>>1)+j][(block_x>>1)+i];
			*fw_mode  = 0;
			*bw_mode  = currMB->b8mode[b8];
		}
		else
		{
			*fw_ref   = fw_refarr[(block_y>>1)+j][(block_x>>1)+i];
			*bw_ref   = bw_refarr[(block_y>>1)+j][(block_x>>1)+i];
			*fw_mode  = currMB->b8mode[b8];
			*bw_mode  = currMB->b8mode[b8];

			if (currMB->b8mode[b8]==0) // direct
			{

				if (img->type==B_IMG)
				{
					//sw
					*fw_ref = 0;// max(0,frefarr[(block_y>>1)+j][(block_x>>1)+i]);
					*bw_ref = 0;
				}
				else
				{
					*fw_ref = max(0,frefarr[(block_y>>1)+j][(block_x>>1)+i]);
					*bw_ref = 0;
				}
			}
		}
	}
}

/*
*************************************************************************
* Function:Residual Coding of a Luma macroblock (not for intra)
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void LumaResidualCoding ()
{
	int i,j,block8x8;
	int fw_mode, bw_mode, refframe;
	int sum_cnt_nonz;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	int incr_y=1,off_y=0;/*lgp*/
	int stage_block8x8_pos=0;/*lgp*/
	int skipped;/*lgp*dct*/
	int sad1 , sad2 ;
	byte** imgY_original = imgY_org;

	currMB->cbp     = 0 ;
	currMB->cbp_blk = 0 ;
	sum_cnt_nonz    = 0 ;

	// !! start shenyanfei 
	if((img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)){
		for (block8x8=stage_block8x8_pos; block8x8<4; block8x8++){
			int bw_ref;
			SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe, &bw_ref);
			LumaPrediction(&(currMB->cbp), &(currMB->cbp_blk), block8x8,fw_mode, bw_mode, refframe, bw_ref);
		}
		sad1 = sad2 = 0 ;
		for (j=0; j < MB_BLOCK_SIZE; j++)
		{
			for (i=0; i < MB_BLOCK_SIZE; i++){
				sad1 += abs(imgY_original[img->pix_y+j][img->pix_x+i] -img->mpr[i][j]);
				sad2 += abs(imgY_original[img->pix_y+j][img->pix_x+i] -img->mpr_weight[i][j]);
			}
		}
		if(sad1 > sad2){
			img->weighting_prediction = 1 ;
		}
		else{
			img->weighting_prediction = 0 ;

⌨️ 快捷键说明

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