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

📄 text_bits.c

📁 MPEG4编解码系统代码
💻 C
📖 第 1 页 / 共 2 页
字号:

/**************************************************************************
该文件包括了纹理部分编码的比特读写操作函数
 *
 **************************************************************************/


#include "momusys.h"
#include "text_defs.h"
#include "bitstream.h"
#include "text_bits.h"
#include "putvlc.h"
#include "zigzag.h"								  /* added, 14-NOV-1996 MW */
#include "max_level.h"							  /* 3-mode esc */

Int  	IntraDC_dpcm (Int val, Int lum, Image *bitstream);
Int  	CodeCoeff (Int j_start, Int Mode, Int qcoeff[],
		Int block, Int ncoeffs, Image *bitstream);
Int  	CodeCoeff_RVLC (Int j_start, Int Mode, Int qcoeff[],
		Int block, Int ncoeffs, Image *bitstream);

/***********************************************************CommentBegin******
 *
 * -- MB_CodeCoeff -- Codes coefficients, does DC/AC prediction
 *
 * Purpose :
 *	Codes coefficients, does DC/AC prediction
 *
 * Arguments in :
 *	Int *qcoeff : quantized dct-coefficients
 * 	Int Mode : encoding mode
 * 	Int CBP : coded block pattern
 * 	Int ncoeffs : number of coefficients per block
 * 	Int x_pos : the horizontal position of the macroblock in the vop
 *      Int intra_dcpred_disable : disable the intradc prediction
 *	Int transp_pattern[]:	Describes which blocks are transparent
 *
 * Arguments out :
 *	Bits *bits : struct to count the number of texture bits
 * 	Image *bitstream : output bitstream
 *
 * Description :
 *	The intradc prediction can be switched off by setting the variable
 *	intradc_pred_disable to '1'.
 *
 ***********************************************************CommentEnd********/
Void MB_CodeCoeff(Bits* bits, Int *qcoeff,
Int Mode, Int CBP, Int ncoeffs,
Int intra_dcpred_disable,
Image *DCbitstream,
Image *bitstream,
Int transp_pattern[], Int direction[],
Int error_res_disable,
Int reverse_vlc,
Int switched,
Int alternate_scan)
{
	Int i, m, coeff[64];
	Int *zz = alternate_scan ? zigzag_v : zigzag;

	if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
	{
		if (intra_dcpred_disable == 0)
		{
			for (i = 0; i < 6; i++)
			{
//				if (i>3 || transp_pattern[i]!=1)  /* Not transparent */
				{
					if (!alternate_scan)
					{
						switch (direction[i])
						{
							case 1: zz = zigzag_v; break;
							case 2: zz = zigzag_h; break;
							case 0: break;
							default: fprintf(stderr, "MB_CodeCoeff(): Error in zigzag direction\n");
							exit(-1);
						}
					}
					/* Do the zigzag scanning of coefficients */
					for (m = 0; m < 64; m++)
					{
						*(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
					}

					if (switched==0)
					{
						if (error_res_disable)
						{
							if (i < 4)
								bits->Y += IntraDC_dpcm(coeff[0],1,bitstream);
							else
								bits->C += IntraDC_dpcm(coeff[0],0,bitstream);
						}
						else
						{
							if (i < 4)
								bits->Y += IntraDC_dpcm(coeff[0],1,DCbitstream);
							else
								bits->C += IntraDC_dpcm(coeff[0],0,DCbitstream);
						}
					}

					/* Code AC coeffs. dep. on block pattern MW 15-NOV-1996 */
					if ((i==0 && CBP&32) ||
						(i==1 && CBP&16) ||
						(i==2 && CBP&8)  ||
						(i==3 && CBP&4)  ||
						(i==4 && CBP&2)  ||
						(i==5 && CBP&1))
					{
						if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
						{
							if (i < 4)
								bits->Y += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,bitstream);
							else
								bits->C += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
									bitstream);
						}
						else
						{
							if (i < 4)
								bits->Y += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
									ncoeffs, bitstream);
							else
								bits->C += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
									ncoeffs, bitstream);
						}
					}
				}
			}
		}
		else									  /* Without ACDC prediction */
		{
			for (i = 0; i < 6; i++)
			{
//				if (i>3 || transp_pattern[i]!=1)  /* Not transparent */
				{
					/* Do the zigzag scanning of coefficients */
					for (m = 0; m < 64; m++)
						*(coeff + zz[m]) = qcoeff[i*ncoeffs+m];

					if (switched==0)
					{
						if (error_res_disable)
						{
							if (coeff[0] != 128)
								BitstreamPutBits(bitstream,(long)(coeff[0]),8L);
							else
								BitstreamPutBits(bitstream, 255L, 8L);
						}
						else
						{
							if (coeff[0] != 128)
								BitstreamPutBits(DCbitstream,(long)(coeff[0]),8L);
							else
								BitstreamPutBits(DCbitstream,255L, 8L);
						}

						if (i < 4)
							bits->Y += 8;
						else
							bits->C += 8;
					}

					if ((i==0 && CBP&32) || (i==1 && CBP&16) ||
						(i==2 && CBP&8) || (i==3 && CBP&4) ||
						(i==4 && CBP&2) || (i==5 && CBP&1))
					{
						/* send coeff, not qcoeff !!! MW 07-11-96 */

						if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
						{
							if (i < 4)
								bits->Y += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
									bitstream);
							else
								bits->C += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
									bitstream);
						}
						else
						{
							if (i < 4)
								bits->Y += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
									ncoeffs, bitstream);
							else
								bits->C += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
									ncoeffs, bitstream);
						}

					}
				}
			}
		}
	}
	else										  /* inter block encoding */
	{
		for (i = 0; i < 6; i++)
		{
			/* Do the zigzag scanning of coefficients */
			for (m = 0; m < 64; m++)
				*(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
			if ((i==0 && CBP&32) ||
				(i==1 && CBP&16) ||
				(i==2 && CBP&8) ||
				(i==3 && CBP&4) ||
				(i==4 && CBP&2) ||
				(i==5 && CBP&1))
			{
				if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
				{
					if (i < 4)
						bits->Y += CodeCoeff(0,Mode, coeff, i, ncoeffs, bitstream);
					else
						bits->C += CodeCoeff(0,Mode, coeff, i, ncoeffs, bitstream);
				}
				else
				{
					if (i < 4)
						bits->Y += CodeCoeff_RVLC(0,Mode, coeff, i, ncoeffs,
							bitstream);
					else
						bits->C += CodeCoeff_RVLC(0,Mode, coeff, i, ncoeffs,
							bitstream);
				}

			}
		}
	}
}


/***********************************************************CommentBegin******
 *
 * -- IntraDC_dpcm -- DPCM Encoding of INTRADC in case of Intra macroblocks
 *
 * Purpose :
 *	DPCM Encoding of INTRADC in case of Intra macroblocks
 *
 * Arguments in :
 *	Int val : the difference value with respect to the previous
 *		INTRADC value
 * 	Int lum : indicates whether the block is a luminance block (lum=1) or
 * 		a chrominance block ( lum = 0)
 *
 * Arguments out :
 *	Image* bitstream  : a pointer to the output bitstream
 *
 ***********************************************************CommentEnd********/

Int
IntraDC_dpcm(Int val, Int lum, Image *bitstream)
{
	Int n_bits;
	Int absval, size = 0;

	absval = ( val <0)?-val:val;				  /* abs(val) */

	/* compute dct_dc_size */

	size = 0;
	while(absval)
	{
		absval>>=1;
		size++;
	}

	if (lum)
	{											  /* luminance */
		n_bits = PutDCsize_lum (size, bitstream);
	}
	else
	{											  /* chrominance */
		n_bits = PutDCsize_chrom (size, bitstream);
	}

	if ( size != 0 )
	{
		if (val>=0)
		{
			;
		}
		else
		{
			absval = -val;						  /* set to "-val" MW 14-NOV-1996 */
			val = (absval ^( (int)pow(2.0,(double)size)-1) );
		}
		BitstreamPutBits(bitstream, (long)(val), (long)(size));
		n_bits += size;

		if (size > 8)
			BitstreamPutBits(bitstream, (long)1, (long)1);
	}

	return n_bits;								  /* # bits for intra_dc dpcm */

}


/***********************************************************CommentBegin******
 *
 * -- CodeCoeff -- VLC encoding of quantized DCT coefficients
 *
 * Purpose :
 *	VLC encoding of quantized DCT coefficients, except for
 *      INTRADC in case of Intra macroblocks
 *	Used by Bits_CountCoeff
 *
 * Arguments in :
 *	Int Mode : encoding mode
 * 	Int *qcoeff: pointer to quantized dct coefficients
 * 	Int block : number of the block in the macroblock (0-5)
 * 	Int ncoeffs : the number of coefficients per block
 *
 * Arguments out :
 *	Image *bitstream : pointer to the output bitstream
 *
 * Return values :
 *	Int bits : number of bits added to the bitstream (?)
 *
 ***********************************************************CommentEnd********/

Int CodeCoeff(Int j_start, Int Mode, Int qcoeff[], Int block, Int ncoeffs, Image *bitstream)
{
	Int j, bits;
	Int prev_run, run, prev_level, level, first;
	Int prev_ind, ind, prev_s, s, length;

	run = bits = 0;
	first = 1;
	prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;

	for (j = j_start; j< ncoeffs; j++)
	{
		/* The INTRADC encoding for INTRA Macroblocks is done in
		   Bits_CountCoeff */

		{
			/* do not enter this part for INTRADC coding for INTRA macro-
			   blocks */

			/* encode AC coeff */

			s = 0;

			/* Increment run if coeff is zero */

			if ((level = qcoeff[j]) == 0)
			{
				run++;
			}
			else
			{
				/* code run & level and count bits */

				if (level < 0)
				{
					s = 1;
					level = -level;
				}

				ind = level | run<<4;
				ind = ind | 0<<12;				  /* Not last coeff */

				if (!first)
				{
					/* Encode the previous ind */

					if ((prev_run < 64) &&
						(((prev_level < 13) && (Mode != MODE_INTRA &&
						Mode != MODE_INTRA_Q))
						|| ((prev_level < 28) && (Mode == MODE_INTRA ||
						Mode == MODE_INTRA_Q))))
					{
						/* Separate tables for Intra luminance blocks */
						if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
						{
							length = PutCoeff_Intra(prev_run, prev_level,
								0, bitstream);
						}
						else
						{
							length = PutCoeff_Inter(prev_run, prev_level,
								0, bitstream);
						}
					}
					else
						length = 0;

					/* First escape mode. Level offset */
					if (length == 0)
					{

						if ( prev_run < 64 )
						{

							/* subtraction of Max level, last = 0 */
							int level_minus_max;

							if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
								level_minus_max = prev_level -
									intra_max_level[0][prev_run];
							else
								level_minus_max = prev_level -
									inter_max_level[0][prev_run];

							if (  ( (level_minus_max < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
								( (level_minus_max < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
							{
								/* Separate tables for Intra luminance blocks */
								if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
								{
									length = PutLevelCoeff_Intra(prev_run, level_minus_max, 0, bitstream);
								}
								else
								{
									length = PutLevelCoeff_Inter(prev_run, level_minus_max, 0, bitstream);
								}
							} else
							length = 0;
						}
						else length = 0;
					}

					/* Second escape mode. Run offset */
					if (length == 0)
					{
						if ( ((prev_level < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q)) ||
							((prev_level < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
						{

⌨️ 快捷键说明

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