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

📄 cabac.c

📁 Mobile IP VCEG的信道模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
*                  intra prediction modes of a given MB. 
*
*
***************************************************************************/

void writeIntraPredMode2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	static int prev_sym = 0;
	static int count = 0; /* to detect a new row of intra prediction modes */

	TextureInfoContexts *ctx = img->currentSlice->tex_ctx;

	if (count % 2 == 0)
		prev_sym = 0;

	unary_bin_max_encode(eep_dp,(unsigned int) se->value1,ctx->ipr_contexts[prev_sym],1,5);
	prev_sym = se->value1;
	unary_bin_max_encode(eep_dp,(unsigned int) se->value2,ctx->ipr_contexts[prev_sym],1,5);
	prev_sym = se->value2;


	if(++count == MB_BLOCK_SIZE/2) /* all modes of one MB have been processed */
		count=0;

}

/****************************************************************************
*
* Function:        writeRefFrame2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode the reference 
*                  parameter of a given MB. 
*
*
***************************************************************************/
void writeRefFrame2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	int a, b;
	int act_ctx;
	int act_sym;
	
	if (currMB->mb_available[0][1] == NULL)
		b = 0;
	else 
		b = ( ((currMB->mb_available[0][1])->ref_frame != 0) ? 1 : 0);
	if (currMB->mb_available[1][0] == NULL)
		a = 0;
	else 
		a = ( ((currMB->mb_available[1][0])->ref_frame != 0) ? 1 : 0);
			
	act_ctx = a + 2*b;
	se->context = act_ctx; // store context
	act_sym = se->value1;
			
	if (act_sym==0) 
	{
		biari_encode_symbol(eep_dp, 0, ctx->ref_no_contexts + act_ctx );
	} 
	else 
	{
		biari_encode_symbol(eep_dp, 1, ctx->ref_no_contexts + act_ctx);
		act_sym--;
		act_ctx=4;
		unary_bin_encode(eep_dp, act_sym,ctx->ref_no_contexts+act_ctx,1);
	}
}

/****************************************************************************
*
* Function:        writeMVD2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode the motion
*                  vector data of a given MB. 
*
*
***************************************************************************/
void writeMVD2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	int step_h, step_v;
	int i = img->subblock_x;
	int j = img->subblock_y;
	int a, b;
	int act_ctx;
	int act_sym;
	int mv_pred_res;
	int mv_local_err;
	int mv_sign;
	int k = se->value2; /* MVD component */
	
	MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	int curr_mb_type = currMB->mb_type;
	

	step_h=input->blc_size[curr_mb_type][0]/BLOCK_SIZE;     
	step_v=input->blc_size[curr_mb_type][1]/BLOCK_SIZE;      
	
	if (j==0)
	{
		if (currMB->mb_available[0][1] == NULL)
			b = 0;
		else 
			b = absm((currMB->mb_available[0][1])->mvd[0][BLOCK_SIZE-1][i][k]);
	}
	else
		b = absm(currMB->mvd[0][j-step_v][i][k]);
					
	if (i==0)
	{
		if (currMB->mb_available[1][0] == NULL)
			a = 0;
		else 
			a = absm((currMB->mb_available[1][0])->mvd[0][j][BLOCK_SIZE-1][k]);
	}
	else
		a = absm(currMB->mvd[0][j][i-step_h][k]);
					
	if ((mv_local_err=a+b)<3)
		act_ctx = 5*k; 
	else
	{
		if (mv_local_err>32)
			act_ctx=5*k+3;
		else
			act_ctx=5*k+2;
	}
	mv_pred_res = se->value1;
	se->context = act_ctx;
					
	act_sym = absm(mv_pred_res);
					
	if (act_sym == 0) 
		biari_encode_symbol(eep_dp, 0, &ctx->mv_res_contexts[0][act_ctx] ); 
	else 
	{
		biari_encode_symbol(eep_dp, 1, &ctx->mv_res_contexts[0][act_ctx] );
		mv_sign = (mv_pred_res<0) ? 1: 0;
		act_ctx=5*k+4;
		biari_encode_symbol(eep_dp, (unsigned char) mv_sign, &ctx->mv_res_contexts[1][act_ctx] );
		act_sym--;
		act_ctx=5*k;
		unary_mv_encode(eep_dp,act_sym,ctx->mv_res_contexts[1]+act_ctx,3);
	}						
}

/****************************************************************************
*
* Function:        writeBiMVD2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode the motion
*                  vector data of a B-frame MB. 
*
*
***************************************************************************/
void writeBiMVD2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	int step_h, step_v;
	int i = img->subblock_x;
	int j = img->subblock_y;
	int a, b;
	int act_ctx;
	int act_sym;
	int mv_pred_res;
	int mv_local_err;
	int mv_sign;
	int backward = se->value2 & 0x01;
	int k = (se->value2>>1); /* MVD component */
	
	MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	//int curr_mb_type = currMB->mb_type;
	
	if(backward == 0) 
	{
		step_h=img->fw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize 
		step_v=img->fw_blc_size_v/BLOCK_SIZE;  // vertical stepsize 
	}
	else
	{
		step_h=img->bw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize 
		step_v=img->bw_blc_size_v/BLOCK_SIZE;  // vertical stepsize   
	}

	if (j==0)
	{
		if (currMB->mb_available[0][1] == NULL)
			b = 0;
		else 
			b = absm((currMB->mb_available[0][1])->mvd[backward][BLOCK_SIZE-1][i][k]);
	}
	else
		b = absm(currMB->mvd[backward][j-step_v][i][k]);
					
	if (i==0)
	{
		if (currMB->mb_available[1][0] == NULL)
			a = 0;
		else 
			a = absm((currMB->mb_available[1][0])->mvd[backward][j][BLOCK_SIZE-1][k]);
	}
	else
		a = absm(currMB->mvd[backward][j][i-step_h][k]);
					
	if ((mv_local_err=a+b)<3)
		act_ctx = 5*k; 
	else
	{
		if (mv_local_err>32)
			act_ctx=5*k+3;
		else
			act_ctx=5*k+2;
	}
	mv_pred_res = se->value1;
	se->context = act_ctx;
					
	act_sym = absm(mv_pred_res);
					
	if (act_sym == 0) 
		biari_encode_symbol(eep_dp, 0, &ctx->mv_res_contexts[0][act_ctx] ); 
	else 
	{
		biari_encode_symbol(eep_dp, 1, &ctx->mv_res_contexts[0][act_ctx] );
		mv_sign = (mv_pred_res<0) ? 1: 0;
		act_ctx=5*k+4;
		biari_encode_symbol(eep_dp, (unsigned char) mv_sign, &ctx->mv_res_contexts[1][act_ctx] );
		act_sym--;
		act_ctx=5*k;
		unary_mv_encode(eep_dp,act_sym,ctx->mv_res_contexts[1]+act_ctx,3);
	}						
}


/****************************************************************************
*
* Function:        writeBiDirBlkSize2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode the forward
*                  or backward bidirectional blocksize (for B frames only) 
*
*
***************************************************************************/
void writeBiDirBlkSize2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	int act_ctx;
	int act_sym;
		
	MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
					
	act_sym = se->value1; // maximum is 6
	act_ctx=4;

	/* using the context models of mb_type */
	unary_bin_max_encode(eep_dp,(unsigned int) act_sym,ctx->mb_type_contexts[1]+act_ctx,1,6);				
}


/****************************************************************************
*
* Function:        writeCBP2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode the coded 
*                  block pattern of a given MB. 
*
*
***************************************************************************/
void writeCBP2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	int mb_x, mb_y;
	int a, b;
	int curr_cbp_ctx, curr_cbp_idx;
	int cbp = se->value1; /* symbol to encode */
	int mask;
	int cbp_bit;
	
	if ( se->type == SE_CBP_INTRA )
		curr_cbp_idx = 0;
	else
		curr_cbp_idx = 1; 
						
	/*  coding of luma part (bit by bit) */
	for (mb_y=0; mb_y < 4; mb_y += 2) 
	{
		for (mb_x=0; mb_x < 4; mb_x += 2)
		{       
					
			if (mb_y == 0)
			{
				if (currMB->mb_available[0][1] == NULL)
					b = 0;
				else 
					b = (( ((currMB->mb_available[0][1])->cbp & (1<<(2+mb_x/2))) == 0) ? 1 : 0);				
			}
			else
				b = ( ((cbp & (1<<(mb_x/2))) == 0) ? 1: 0);
					
			if (mb_x == 0)
			{
				if (currMB->mb_available[1][0] == NULL)
					a = 0;
				else 
					a = (( ((currMB->mb_available[1][0])->cbp & (1<<(mb_y+1))) == 0) ? 1 : 0);								
			}
			else
				a = ( ((cbp & (1<<mb_y)) == 0) ? 1: 0);

			curr_cbp_ctx = a+2*b;	
			mask = (1<<(mb_y+mb_x/2));
			cbp_bit = cbp & mask;	
			biari_encode_symbol(eep_dp, (unsigned char) cbp_bit, ctx->cbp_contexts[curr_cbp_idx][0] + curr_cbp_ctx );
		}
	}
		
	/* coding of chroma part */
	b = 0;
	if (currMB->mb_available[0][1] != NULL)
		b = ((currMB->mb_available[0][1])->cbp > 15) ? 1 : 0;
			
	a = 0;
	if (currMB->mb_available[1][0] != NULL)
		a = ((currMB->mb_available[1][0])->cbp > 15) ? 1 : 0;
						
	curr_cbp_ctx = a+2*b;					 
	cbp_bit = (cbp > 15 ) ? 1 : 0;
	biari_encode_symbol(eep_dp, (unsigned char) cbp_bit, ctx->cbp_contexts[curr_cbp_idx][1] + curr_cbp_ctx );
			
	if (cbp > 15)
	{
		b = 0;
		if (currMB->mb_available[0][1] != NULL)
			if ((currMB->mb_available[0][1])->cbp > 15)
				b = (( ((currMB->mb_available[0][1])->cbp >> 4) == 2) ? 1 : 0);

		a = 0;
		if (currMB->mb_available[1][0] != NULL)
			if ((currMB->mb_available[1][0])->cbp > 15)
				a = (( ((currMB->mb_available[1][0])->cbp >> 4) == 2) ? 1 : 0);
				
		curr_cbp_ctx = a+2*b;
		cbp_bit = ((cbp>>4) == 2) ? 1 : 0;
		biari_encode_symbol(eep_dp, (unsigned char) cbp_bit, ctx->cbp_contexts[curr_cbp_idx][2] + curr_cbp_ctx );			
	}			
}


/****************************************************************************
*
* Function:        writeRunLevel2Buffer_CABAC()
*
* Purpose:         This function is used to arithmetically encode level and 
*                  run of a given MB. 
*
*
***************************************************************************/
void writeRunLevel2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
	const int level = se->value1;
	const int run = se->value2;
	const int curr_ctx_idx = se->context;
	int curr_level_ctx;
	int sign_of_level;
	int max_run;

	TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
	//Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	unary_level_encode(eep_dp,(unsigned int) absm(level),ctx->level_context[curr_ctx_idx]);
											
	if (level!=0)
	{
		sign_of_level = ((level < 0) ? 1 : 0);
		curr_level_ctx = 3;														
		biari_encode_symbol(eep_dp, (unsigned char) sign_of_level, ctx->level_context[curr_ctx_idx] + curr_level_ctx );	
		if (curr_ctx_idx != 0 && curr_ctx_idx != 6 && curr_ctx_idx != 5) // not double scan and not DC-chroma
		//if (curr_ctx_idx != 0 && curr_ctx_idx != 3) // not double scan and not DC-chroma
				unary_bin_encode(eep_dp,(unsigned int) run,ctx->run_context[curr_ctx_idx],1);
		else
		{
			max_run =  (curr_ctx_idx == 0) ? 7 : 3;  // if double scan max_run = 7; if DC-chroma max_run = 3;
			unary_bin_max_encode(eep_dp,(unsigned int) run,ctx->run_context[curr_ctx_idx],1,max_run);
		}
	}

}
/************************************************************************
*
*  Name :       unary_bin_encode()
*
*  Description: Unary binarization and encoding of a symbol by using 
*								one or two distinct models for the first two and all 
*								remaining bins
*
************************************************************************/
void unary_bin_encode(EncodingEnvironmentPtr eep_dp,
											unsigned int symbol,
											BiContextTypePtr ctx,
											int ctx_offset)
{ 
	unsigned int l;
	BiContextTypePtr ictx;

	if (symbol==0) 
	{
		biari_encode_symbol(eep_dp, 0, ctx );
		return;
	} 
	else 
	{
		biari_encode_symbol(eep_dp, 1, ctx );
		l=symbol;
		ictx=ctx+ctx_offset;
		while ((--l)>0)
			biari_encode_symbol(eep_dp, 1, ictx);
		biari_encode_symbol(eep_dp, 0, ictx);	
	}  
	return;
}

/************************************************************************
*
*  Name :       unary_bin_max_encode()
*
*  Description: Unary binarization and encoding of a symbol by using 
*								one or two distinct models for the first two and all 
*								remaining bins; no terminating "0" for max_symbol 
*								(finite symbol alphabet)
*
************************************************************************/
void unary_bin_max_encode(EncodingEnvironmentPtr eep_dp,
													unsigned int symbol,
													BiContextTypePtr ctx,
													int ctx_offset,
													unsigned int max_symbol)
{ 
	unsigned int l;
	BiContextTypePtr ictx;

	if (symbol==0) 
	{
		biari_encode_symbol(eep_dp, 0, ctx );
		return;
	} 
	else 
	{
		biari_encode_symbol(eep_dp, 1, ctx );
		l=symbol;
		ictx=ctx+ctx_offset;
		while ((--l)>0)
			biari_encode_symbol(eep_dp, 1, ictx);
		if (symbol<max_symbol)
				biari_encode_symbol(eep_dp, 0, ictx);	
	}  
	return;
}

/************************************************************************
*
*  Name :       unary_level_encode()
*
*  Description: Unary binarization and encoding of a symbol by using 
*								three distinct models for the first, the second and all 
*								remaining bins
*
************************************************************************/
void unary_level_encode(EncodingEnvironmentPtr eep_dp,
												unsigned int symbol,
												BiContextTypePtr ctx)
{ 
	unsigned int l;
	int bin=1;
	BiContextTypePtr ictx=ctx;

	if (symbol==0) 
	{
		biari_encode_symbol(eep_dp, 0, ictx );
		return;
	} 
	else 
	{
		biari_encode_symbol(eep_dp, 1, ictx );
		l=symbol;
		ictx++;
		while ((--l)>0)
		{
			biari_encode_symbol(eep_dp, 1, ictx  );
			if ((++bin)==2) ictx++;
		}
		biari_encode_symbol(eep_dp, 0, ictx  );	
	}  
	return;
}

/************************************************************************
*
*  Name :       unary_mv_encode()
*
*  Description: Unary binarization and encoding of a symbol by using 
*								four distinct models for the first, the second, intermediate
*								and all remaining bins
*
************************************************************************/
void unary_mv_encode(EncodingEnvironmentPtr eep_dp,
												unsigned int symbol,
												BiContextTypePtr ctx,
												unsigned int max_bin)
{ 
	unsigned int l;
	unsigned int bin=1;
	BiContextTypePtr ictx=ctx;

	if (symbol==0) 
	{
		biari_encode_symbol(eep_dp, 0, ictx );
		return;
	} 
	else 
	{
		biari_encode_symbol(eep_dp, 1, ictx );
		l=symbol;
		ictx++;
		while ((--l)>0)
		{
			biari_encode_symbol(eep_dp, 1, ictx  );
			if ((++bin)==2) ictx++;
			if (bin==max_bin) ictx++;
		}	
		biari_encode_symbol(eep_dp, 0, ictx  );	
	}  
	return;
}



⌨️ 快捷键说明

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