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

📄 predict.c

📁 代码实现了基于ARM7的MPEG-4视频解码器
💻 C
字号:
//*********************************************
//File name: predict.c
//Author:    Anna
//Date:
//*********************************************
#define DEC_MBC         45
#define DEC_MBR         36
#define TOP 1
#define LEFT 0
#define P_VOP		1
#define INTRA			3
#define INTRA_Q	 	4


#define abs(a)	((a)>0 ? (a) : -(a))
#define _div_div(a, b)	(a>0) ? (a+(b>>1))/b : (a-(b>>1))/b

extern int dc_store_lum[2*DEC_MBR+1][2*DEC_MBC+1];
extern int ac_left_lum[2*DEC_MBR+1][2*DEC_MBC+1][7];
extern int ac_top_lum[2*DEC_MBR+1][2*DEC_MBC+1][7];

extern int dc_store_chr[2][DEC_MBR+1][DEC_MBC+1];
extern int ac_left_chr[2][DEC_MBR+1][DEC_MBC+1][7];
extern int ac_top_chr[2][DEC_MBR+1][DEC_MBC+1][7];
extern int	quant_store[DEC_MBR+1][DEC_MBC+1]; // [Review]
extern int	modemap[DEC_MBR+1][DEC_MBC+2];

	
extern int predict_dir;

extern int mb_xpos;
extern int mb_ypos;
extern int dc_scaler;
extern int quantizer;
extern int ac_pred_flag;
extern int prediction_type;


static void rescue_predict();



void dc_recon(int block_num, short * dc_value){
    
     if (prediction_type == P_VOP) {
		rescue_predict();
	}
	
	if (block_num < 4)
	{
		int b_xpos = (mb_xpos << 1) + (block_num & 1);
		int b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
		int dc_pred;

		// set prediction direction
		if (abs(dc_store_lum[b_ypos+1-1][b_xpos+1-1] -
			dc_store_lum[b_ypos+1][b_xpos+1-1]) < // Fa - Fb
			abs(dc_store_lum[b_ypos+1-1][b_xpos+1-1] -
			dc_store_lum[b_ypos+1-1][b_xpos+1])) // Fb - Fc
			{
				predict_dir = TOP;
				dc_pred = dc_store_lum[b_ypos+1-1][b_xpos+1];
			}
		else
			{
				predict_dir = LEFT;
				dc_pred = dc_store_lum[b_ypos+1][b_xpos+1-1];
			}

		(* dc_value) += _div_div(dc_pred, dc_scaler);
		(* dc_value) *= dc_scaler;
       /*Anna*/ 
		if(*dc_value>2047)
			*dc_value=2047;
		if(*dc_value<-2048)
			*dc_value=-2048;	// store dc value
		dc_store_lum[b_ypos+1][b_xpos+1] = (* dc_value);
	
	}
	else // chrominance blocks
	{
		int b_xpos = mb_xpos;
		int b_ypos = mb_ypos;
		int chr_num = block_num - 4;
		int dc_pred;

		// set prediction direction
		if (abs(dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1-1] -
			dc_store_chr[chr_num][b_ypos+1][b_xpos+1-1]) < // Fa - Fb
			abs(dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1-1] -
			dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1])) // Fb - Fc
			{
				predict_dir = TOP;
				dc_pred = dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1];
			}
		else
			{
				predict_dir = LEFT;
				dc_pred = dc_store_chr[chr_num][b_ypos+1][b_xpos+1-1];
			}

		(* dc_value) += _div_div(dc_pred, dc_scaler);
		(* dc_value) *= dc_scaler;
		/*Anna*/
        (* dc_value)=((* dc_value)>2047) ? 2047 :( ((* dc_value)<-2048) ? 2048 :(* dc_value));
		// store dc value
		dc_store_chr[chr_num][b_ypos+1][b_xpos+1] = (* dc_value);
	}
	
}//first end

static int saiAcLeftIndex[8] = 
{
	0, 8,16,24,32,40,48,56
};

/***/
void ac_recon(int block_num, short * psBlock)
{
	int b_xpos, b_ypos;
	int i;

	if (block_num < 4) {
		b_xpos = (mb_xpos << 1) + (block_num & 1);
		b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
	}
	else {
		b_xpos = mb_xpos;
		b_ypos = mb_ypos;
	}

	// predict coefficients
	if (ac_pred_flag) 
	{
		if (block_num < 4) 
		{
			if (predict_dir == TOP)
			{
				for (i = 1; i < 8; i++) // [Review] index can become more efficient [0..7]
					psBlock[i] += ac_top_lum[b_ypos+1-1][b_xpos+1][i-1];
			}
			else // left prediction
			{
				for (i = 1; i < 8; i++)
					psBlock[saiAcLeftIndex[i]] += ac_left_lum[b_ypos+1][b_xpos+1-1][i-1];
			}
		}
		else
		{
			int chr_num = block_num - 4;

			if (predict_dir == TOP)
			{
				for (i = 1; i < 8; i++)
					psBlock[i] += ac_top_chr[chr_num][b_ypos+1-1][b_xpos+1][i-1];
			}
			else // left prediction
			{
				for (i = 1; i < 8; i++)
					psBlock[saiAcLeftIndex[i]] += ac_left_chr[chr_num][b_ypos+1][b_xpos+1-1][i-1];
			}
		}
	}
}

/***/
void ac_store(int block_num, short * psBlock)
{
	int b_xpos, b_ypos;
	int i;

	// [Review] This lines of code are repeated frequently
	if (block_num < 4) { 
		b_xpos = (mb_xpos << 1) + (block_num & 1);
		b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
	}
	else {
		b_xpos = mb_xpos;
		b_ypos = mb_ypos;
	}

	// store coefficients
	if (block_num < 4)
	{
		for (i = 1; i < 8; i++) {
			ac_top_lum[b_ypos+1][b_xpos+1][i-1] = psBlock[i];
			ac_left_lum[b_ypos+1][b_xpos+1][i-1] = psBlock[saiAcLeftIndex[i]];
		}
	}
	else 
	{
		int chr_num = block_num - 4;

		for (i = 1; i < 8; i++) {
			ac_top_chr[chr_num][b_ypos+1][b_xpos+1][i-1] = psBlock[i];
			ac_left_chr[chr_num][b_ypos+1][b_xpos+1][i-1] = psBlock[saiAcLeftIndex[i]];
		}
	}
}

/***/

#define _rescale(predict_quant, current_quant, coeff)	(coeff != 0) ?	\
_div_div((coeff) * (predict_quant), (current_quant))	: 0

int ac_rescaling(int block_num, short * psBlock)
{
	int mb_xpos_here = mb_xpos;
	int mb_ypos_here = mb_ypos;
	int current_quant = quantizer;
	int predict_quant = (predict_dir == TOP) ?
		quant_store[mb_ypos_here][mb_xpos_here+1] : quant_store[mb_ypos_here+1][mb_xpos_here];
	int b_xpos, b_ypos; // index for stored coeff matrix
	int i;

	if ((! ac_pred_flag) || (current_quant == predict_quant) || (block_num == 3))
		return 0;

	if ((mb_ypos_here == 0) && (predict_dir == TOP))
		return 0;
	if ((mb_xpos_here == 0) && (predict_dir == LEFT))
		return 0;
	if ((mb_xpos_here == 0) && (mb_ypos_here == 0))
		return 0;

	// [Review] This lines of code are repeated frequently
	if (block_num < 4) {
		b_xpos = (mb_xpos << 1) + (block_num & 1);
		b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
	}
	else {
		b_xpos = mb_xpos;
		b_ypos = mb_ypos;
	}

	if (predict_dir == TOP) // rescale only if really needed
	{
		switch (block_num)
		{
		case 0: case 1:
			for (i = 1; i < 8; i++)
				psBlock[i] += _rescale(predict_quant, current_quant, ac_top_lum[b_ypos][b_xpos+1][i-1]);
			return 1;
			break;
		case 4:
			for (i = 1; i < 8; i++)
				psBlock[i] += _rescale(predict_quant, current_quant, ac_top_chr[0][b_ypos][b_xpos+1][i-1]);
			return 1;
			break;
		case 5:
			for (i = 1; i < 8; i++)
				psBlock[i] += _rescale(predict_quant, current_quant, ac_top_chr[1][b_ypos][b_xpos+1][i-1]);
			return 1;
			break;
		}
	}
	else 
	{
		switch (block_num)
		{
		case 0: case 2:
			for (i = 1; i < 8; i++)
				psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_lum[b_ypos+1][b_xpos][i-1]);
			return 1;
			break;
		case 4:
			for (i = 1; i < 8; i++)
				psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_chr[0][b_ypos+1][b_xpos][i-1]);
			return 1;
			break;
		case 5:
			for (i = 1; i < 8; i++)
				psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_chr[1][b_ypos+1][b_xpos][i-1]);
			return 1;
			break;
		}
	}

	return 0;
}

/***/

#define _IsIntra(mb_y, mb_x) ((modemap[(mb_y)+1][(mb_x)+1] == INTRA) || \
	(modemap[(mb_y)+1][(mb_x)+1] == INTRA_Q))

static void rescue_predict() 
{
	//int mb_xpos = mp4_state->hdr.mb_xpos;
	//int mb_ypos = mp4_state->hdr.mb_ypos;
	int i;

	if (! _IsIntra(mb_ypos-1, mb_xpos-1)) {
		// rescue -A- DC value
		 dc_store_lum[2*mb_ypos+1-1][2*mb_xpos+1-1] = 1024;
		 dc_store_chr[0][mb_ypos+1-1][mb_xpos+1-1] = 1024;
		 dc_store_chr[1][mb_ypos+1-1][mb_xpos+1-1] = 1024;
	}
	// left
	if (! _IsIntra(mb_ypos, mb_xpos-1)) {
		// rescue -B- DC values
		 dc_store_lum[2*mb_ypos+1][2*mb_xpos+1-1] = 1024;
		 dc_store_lum[2*mb_ypos+1+1][2*mb_xpos+1-1] = 1024;
		 dc_store_chr[0][mb_ypos+1][mb_xpos+1-1] = 1024;
		 dc_store_chr[1][mb_ypos+1][mb_xpos+1-1] = 1024;
		//  rescue -B- AC values
		for(i = 0; i < 7; i++) {
			 ac_left_lum[2*mb_ypos+1][2*mb_xpos+1-1][i] = 0;
			 ac_left_lum[2*mb_ypos+1+1][2*mb_xpos+1-1][i] = 0;
			 ac_left_chr[0][mb_ypos+1][mb_xpos+1-1][i] = 0;
			 ac_left_chr[1][mb_ypos+1][mb_xpos+1-1][i] = 0;
		}
	}
	// top
	if (! _IsIntra(mb_ypos-1, mb_xpos)) {
		// rescue -C- DC values
		 dc_store_lum[2*mb_ypos+1-1][2*mb_xpos+1] = 1024;
		 dc_store_lum[2*mb_ypos+1-1][2*mb_xpos+1+1] = 1024;
		 dc_store_chr[0][mb_ypos+1-1][mb_xpos+1] = 1024;
		 dc_store_chr[1][mb_ypos+1-1][mb_xpos+1] = 1024;
		// rescue -C- AC values
		for(i = 0; i < 7; i++) {
			 ac_top_lum[2*mb_ypos+1-1][2*mb_xpos+1][i] = 0;
			 ac_top_lum[2*mb_ypos+1-1][2*mb_xpos+1+1][i] = 0;
			 ac_top_chr[0][mb_ypos+1-1][mb_xpos+1][i] = 0;
			 ac_top_chr[1][mb_ypos+1-1][mb_xpos+1][i] = 0;
		}
	}
}




⌨️ 快捷键说明

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