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

📄 text_code.c

📁 MPEG4视频编解码内有divx(编码)
💻 C
📖 第 1 页 / 共 2 页
字号:

/**************************************************************************
图象纹理编码函数代码
 *
 **************************************************************************/


#include "text_defs.h"
#include "mot_code.h"
#include "bitstream.h"
#include "putvlc.h"
#include "mot_util.h"
#include "text_code_mb.h"
#include "text_code.h"

#define SKIPP       6

extern FILE *ftrace;

Void  	Bits_CountMB_combined (	Int DQUANT,
			Int Mode,
			Int COD,
			Int ACpred_flag,
			Int CBP,
			Int vop_type,
			Bits *bits,
			Image *mottext_bitstream,
			Int *MB_transp_pattern
	);
Int  	doDCACpred (	Int *qcoeff,
			Int *CBP,
			Int ncoeffs,
			Int x_pos,
			Int y_pos,
			Int ***DC_store,
			Int QP,
			Int MB_width,
			Int direction[],
			Int mid_grey
	);
Void nullfill(Int pred[], Int mid_grey);
Int Idir_c(Int val, Int QP);
Int  	IntraDCSwitch_Decision _P_((	Int Mode,
			Int intra_dc_vlc_thr,
			Int Qp
	));
Int  	FindCBP _P_((	Int *qcoeff,
			Int Mode,
			Int ncoeffs
	));


/***********************************************************CommentBegin******
函数功能:
本函数实现的是一个视频对象平面的帧内纹理编码,它包括了组合形状编码模式和纹理编码模式。
函数输入:
1)Vop curr:当前将要进行编码的视频对象平面;
2)Int intra_dcpred_disable:是否不允许帧内DC频段预测控制;
3)Image* AB_SizeConversionDecisions:视频对象平面转换为视频宏块过程中的块大小;
4)Image* AB_first_MMR_values:最大宏块阈值;
5)VolConfig *vol_config:设置信息;
6)Int rc_type:比特率控制类型。
函数输出:
1)Vop *rec_curr:当前重建的视频对象平面;
2)Image *texture_bitstream:输出比特流;
3)Bits* bits:数据统计信息。
函数描述:
该函数实现了同一视频对象平面的帧内图像纹理的编码。

 ***********************************************************CommentEnd********/
Void VopCodeShapeTextIntraCom(Vop *curr, Vop *rec_curr, Image *mottext_bitstream)
{
	Int QP = GetVopIntraQuantizer(curr);
	Int Mode = MODE_INTRA;
	Int* qcoeff;
	Int i, j;
	Int CBP, COD;
	Int CBPY, CBPC;
	Int num_pixels = GetImageSizeX(GetVopY(curr));
	Int num_lines = GetImageSizeY(GetVopY(curr));
	Int vop_type;
	Int ***DC_store;
	Int MB_width = num_pixels / MB_SIZE;
	Int MB_height = num_lines / MB_SIZE;
	Int m;
	Int ACpred_flag=-1;
	Int direction[6];
	Int switched=0;
	Int DQUANT =0;

	Bits nbits, *bits;
	bits = &nbits;

	qcoeff = (Int *) malloc (sizeof (Int) * 384);

	#ifdef _RC_DEBUG_
	fprintf(ftrace, "RC - VopCodeShapeTextIntraCom(): ---> CODING WITH: %d \n",QP);
	#endif

	for (i = 0; i < 6; i++)
		direction[i] = 0;

	/*给3D矩阵分配空间,矩阵是用来跟踪为DC或AC频段预测服务的预测值*/

	DC_store = (Int ***)calloc(MB_width*MB_height, sizeof(Int **));
	for (i = 0; i < MB_width*MB_height; i++)
	{
		DC_store[i] = (Int **)calloc(6, sizeof(Int *));
		for (j = 0; j < 6; j++)
			DC_store[i][j] = (Int *)calloc(15, sizeof(Int));
	}

	Bits_Reset (bits);
	vop_type = PCT_INTRA;

	for (j = 0; j < num_lines/MB_SIZE; j++)		  /* 宏块循环*/
	{
		for (i = 0; i < num_pixels/MB_SIZE; i++)
		{
			DQUANT = 0;

			COD = 0;
			bits->no_intra++;

			CodeMB (curr, rec_curr, NULL, i*MB_SIZE, j*MB_SIZE,
				num_pixels, QP+DQUANT, MODE_INTRA, qcoeff);

			m =0;

			DC_store[j*MB_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2);
			DC_store[j*MB_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2);
				
			for (m = 1; m < 8; m++)
			{
				DC_store[j*MB_width+i][0][m] = qcoeff[m];
				DC_store[j*MB_width+i][1][m] = qcoeff[m+64];
				DC_store[j*MB_width+i][2][m] = qcoeff[m+128];
				DC_store[j*MB_width+i][3][m] = qcoeff[m+192];
				DC_store[j*MB_width+i][4][m] = qcoeff[m+256];
				DC_store[j*MB_width+i][5][m] = qcoeff[m+320];
			}
			for (m = 0; m < 7; m++)
			{
				DC_store[j*MB_width+i][0][m+8] = qcoeff[(m+1)*8];
				DC_store[j*MB_width+i][1][m+8] = qcoeff[(m+1)*8+64];
				DC_store[j*MB_width+i][2][m+8] = qcoeff[(m+1)*8+128];
				DC_store[j*MB_width+i][3][m+8] = qcoeff[(m+1)*8+192];
				DC_store[j*MB_width+i][4][m+8] = qcoeff[(m+1)*8+256];
				DC_store[j*MB_width+i][5][m+8] = qcoeff[(m+1)*8+320];
			}

			CBP = FindCBP(qcoeff,Mode,64);

			/*进行DC或AC频段的预测,适当的改变量化系数的值*/
			if (GetVopIntraACDCPredDisable(curr) == 0)
			{
				ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j, DC_store,
					QP+DQUANT, MB_width,
					direction,GetVopMidGrey(curr));
			}
			else
				ACpred_flag = -1;

			switched = IntraDCSwitch_Decision(Mode,
				GetVopIntraDCVlcThr(curr),
				QP);
			if (switched)
				CBP = FindCBP(qcoeff,MODE_INTER,64);
			if (DQUANT) Mode=MODE_INTRA_Q;else Mode=MODE_INTRA;

			QP+=DQUANT;

			CBPY = CBP >> 2;
			CBPY = CBPY & 15;			  /*最后4比特位*/
			CBPC = CBP & 3;				  /*最后2比特位*/

			Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP,
				vop_type,
				bits, mottext_bitstream,/*MB_transp_pattern*/NULL);

			/*附加上变量intra_dcpred_diable */
			MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64,
				GetVopIntraACDCPredDisable(curr),
				NULL, mottext_bitstream,
				NULL, direction,
				1 /*如何得到的呢:GetVopErrorResDisable(curr)*/,
				0 /*如何得到的呢:GetVopReverseVlc(curr)*/,
				switched,
				0 /*0表示:当前视频对象平面交替扫描控制变量的取值*/);
		}
	}

	/*释放已分配给3D矩阵的内存空间*/
	for (i = 0; i < MB_width*MB_height; i++)
	{
		for (j = 0; j < 6; j++)
			free(DC_store[i][j]);
		free(DC_store[i]);
	}
	free(DC_store);

	free ((Char*)qcoeff);
}


/***********************************************************CommentBegin******
 *
函数功能:
运动形状纹理编码。
函数输入:
1)Vop* curr:当前需编码的视频对象平面;
2)Vop* rec_prev:先前已重建的视频对象平面;
3)Image* mot_x:运动向量的x轴坐标值;
4)Image* mot_y:运动向量的y轴坐标值;
5)Image* MB_decisions:包含了每一个宏块的编码模式;
6)Int f_code_for:运动向量的搜索范围;
3)Image* AB_SizeConversionDecisions:视频对象平面转换为视频宏块过程中的块大小;
4)Image* AB_first_MMR_values:最大宏块阈值;
9)Int intra_dcpred_disable:不允许帧内DC频段预测的控制变量;
10)VolConfig* vol_config:设置信息;
11)Int rc_type:比特率控制类型。
函数输出:
1)Vop* rec_curr:当前重建的视频对象平面;
2)Image* mottext_bitstream:输出的纹理/运动信息编码比特流;
3)Bits *bits:编码统计信息。

 ***********************************************************CommentEnd********/

Void VopShapeMotText (Vop *curr, Vop *comp, Image* MB_decisions, Image* mot_x, Image *mot_y,
				Int f_code_for, Int intra_acdc_pred_disable, Vop *rec_curr, Image *mottext_bitstream)
{
	Int Mode=0;
	Int QP = GetVopQuantizer(curr);
	Int* qcoeff=NULL;
	Int i, j;
	Int CBP;
	Int COD;
	Int CBPY, CBPC;
	Int MB_in_width, MB_in_height, B_in_width, mbnum, boff;
	SInt p;
	SInt *ptr=NULL;
	Float *motx_ptr=NULL, *moty_ptr=NULL;
	Int num_pixels;
	Int num_lines;
	Int vop_type=PCT_INTER;
	Int ***DC_store=NULL;
	Int m, n;
	Int ACpred_flag=-1;
	Int direction[6];
	Int switched=0;
	Int DQUANT=0;
	Bits nbits, *bits;
	bits = &nbits;
	qcoeff = (Int *) malloc (sizeof (Int) * 384);
	num_pixels = GetImageSizeX(GetVopY(curr));
	num_lines = GetImageSizeY(GetVopY(curr));
	MB_in_width = num_pixels / MB_SIZE;
	MB_in_height = num_lines / MB_SIZE;
	B_in_width = 2 * MB_in_width;

	for (i = 0; i < 6; i++) direction[i] = 0;
	#ifdef _RC_DEBUG_
	printf("RC - VopShapeMotText(): ---> CODING WITH: %d \n",QP);
	#endif
	/*给3D矩阵分配空间,矩阵是用来跟踪为DC或AC频段预测服务的预测值*/
	DC_store = (Int ***)calloc(MB_in_width*MB_in_height,
		sizeof(Int **));
	for (i = 0; i < MB_in_width*MB_in_height; i++)
	{
		DC_store[i] = (Int **)calloc(6, sizeof(Int *));
		for (j = 0; j < 6; j++)
			DC_store[i][j] = (Int *)calloc(15, sizeof(Int));
	}
	Bits_Reset (bits);
	vop_type = PCT_INTER;
	ptr = (SInt *) GetImageData(MB_decisions);
	motx_ptr = (Float *) GetImageData(mot_x);
	moty_ptr = (Float *) GetImageData(mot_y);
	for (j = 0; j < num_lines/MB_SIZE; j++)
	{
		for (i = 0; i < MB_in_width; i++)
		{
			switched=0;
			p = *ptr;
			DQUANT = 0;
			/*用缺省的系数值对DC频段进行填充*/
			for (m = 0; m < 6; m++)
			{
				DC_store[j*MB_in_width+i][m][0] = GetVopMidGrey(curr)*8;
				for (n = 1; n < 15; n++)
					DC_store[j*MB_in_width+i][m][n] = 0;
			}
			switch (p)
			{
				case MBM_INTRA:
					Mode = (DQUANT == 0) ? MODE_INTRA : MODE_INTRA_Q;
					bits->no_intra++;
					break;
				case MBM_INTER16:
					Mode = (DQUANT == 0) ? MODE_INTER : MODE_INTER_Q;
					bits->no_inter++;
					break;
				case MBM_INTER8:
					Mode = MODE_INTER4V;
					bits->no_inter4v++;
					DQUANT = 0;				/*这里不可为8×8模式而改变QP值*/
					break;

				default:
					printf("invalid MB_decision value :%d\n", p);
					exit(0);
			}

			CodeMB (curr, rec_curr, comp, i*MB_SIZE, j*MB_SIZE,
				num_pixels, QP + DQUANT, Mode, qcoeff);

			mbnum  = j*MB_in_width + i;
			boff = (2 * (mbnum  / MB_in_width) * B_in_width
				+ 2 * (mbnum % MB_in_width));

			CBP = FindCBP(qcoeff,Mode,64);

			if ((CBP == 0) && (p == 1) && (*(motx_ptr +boff) == 0.0)
				&& (*(moty_ptr +boff) == 0.0))
			{
				COD = 1;				  /*跳过的宏块*/
				BitstreamPutBits(mottext_bitstream, (long) (COD), 1L);
				bits->COD ++;

				*ptr = SKIPP;
				Mode = MODE_INTER;
			}
			else
			{
				COD = 0;				  /*已编码的宏块*/
				if ((Mode == MODE_INTRA) || (Mode == MODE_INTRA_Q))
				{
					/*存储以后预测所需要的量化系数值*/
					m =0;
			DC_store[j*MB_in_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1);
			DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2);
			DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2);
					for (m = 1; m < 8; m++)
					{
						DC_store[j*MB_in_width+i][0][m] = qcoeff[m];
						DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64];
						DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128];
						DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192];
						DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256];
						DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320];
					}
					for (m = 0; m < 7; m++)
					{
						DC_store[j*MB_in_width+i][0][m+8] = qcoeff[(m+1)*8];
						DC_store[j*MB_in_width+i][1][m+8] = qcoeff[(m+1)*8+64];
						DC_store[j*MB_in_width+i][2][m+8] = qcoeff[(m+1)*8+128];
						DC_store[j*MB_in_width+i][3][m+8] = qcoeff[(m+1)*8+192];
						DC_store[j*MB_in_width+i][4][m+8] = qcoeff[(m+1)*8+256];
						DC_store[j*MB_in_width+i][5][m+8] = qcoeff[(m+1)*8+320];
					}
					if (intra_acdc_pred_disable == 0)
						ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j,
							DC_store,
							QP+DQUANT, MB_in_width,
							direction,GetVopMidGrey(curr));
					else
						ACpred_flag = -1;  /*标志并不编入比特流中*/
				}
				switched = IntraDCSwitch_Decision(Mode,
					GetVopIntraDCVlcThr(curr),
					QP);
				if (switched)
					CBP = FindCBP(qcoeff,MODE_INTER,64);
				CBPY = CBP >> 2;
				CBPY = CBPY & 15;		  /*最后4个比特位*/
				CBPC = CBP & 3;			  /*最后2个比特位*/
				Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP,
					vop_type, bits,
					mottext_bitstream, NULL);
				Bits_CountMB_Motion( mot_x, mot_y, NULL, 
					MB_decisions, i, j, f_code_for, 0,
					mottext_bitstream,
					1, 0, (Int **)NULL, 0);
				MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64,
					intra_acdc_pred_disable,
					NULL, mottext_bitstream,
					NULL, direction,
					1/*如何得到的呢:GetVopErrorResDisable(curr)*/,
					0/*如何得到的呢:GetVopReverseVlc(curr)*/,
					switched,
					0;
			}
			ptr++;
		} /*控制变量i的循环*/
	} /*控制变量j的循环*/
	/* 释放为3D矩阵分配的内存空间 */
	for (i = 0; i < MB_in_width*MB_in_height; i++)
	{
		for (j = 0; j < 6; j++)
			free(DC_store[i][j]);
		free(DC_store[i]);
	}
	free(DC_store);
	free ((Char*)qcoeff);
}


/***********************************************************CommentBegin******
 *
 * -- Bits_CountMB_combined -- texture encoding for combined texture/motion
 *
 * Purpose :
 *	Used for texture encoding in case of combined texture/motion
 * 		encoding. This function encodes the :
 *		- COD flag
 *		- MCBPC flag
 *		- CBPY flag
 *		- CBPC flag
 *		- DQUANT information
 *
 * Arguments in :
 *	SInt Mode : The macroblock encoding mode
 * 	Int CBP : Coded block pattern information
 * 	Int COD : Indicates whether this macroblock is coded or not
 *	Int ACpred_flag
 * 	Int vop_type : indicates the picture coding type
 *		(Intra,Inter)
 *
 * Arguments out :
 *	Bits* bits : a structure counting the number of bits
 * 	Image *bitstream : output texture bit stream *
 *
 ***********************************************************CommentEnd********/

⌨️ 快捷键说明

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