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

📄 mpeg4quant.c

📁 DM642的mpeg4编码
💻 C
字号:
#include "../mpeg4/mpeg4.h"
#include "../quant/mpeg4quant.h"
#include "../bitstream/scan.h"
#include "../global.h"

#define VM18P 3
#define VM18Q 4

#define SCALEBITS	16
#define FIX(X)		((1L << SCALEBITS) / (X) + 1)

static const unsigned int multipliers[32] =
{
	0,       FIX(2),  FIX(4),  FIX(6),
	FIX(8),  FIX(10), FIX(12), FIX(14),
	FIX(16), FIX(18), FIX(20), FIX(22),
	FIX(24), FIX(26), FIX(28), FIX(30),
	FIX(32), FIX(34), FIX(36), FIX(38),
	FIX(40), FIX(42), FIX(44), FIX(46),
	FIX(48), FIX(50), FIX(52), FIX(54),
	FIX(56), FIX(58), FIX(60), FIX(62)
};

void LoadCustomMpeg4IntraMatrix(Stream* bs, unsigned char* intra_quant_mate)
{
	int i, j;

	const int last = intra_quant_mate[Table[0][63]];

	for (j = 63; j > 0 && intra_quant_mate[Table[0][j - 1]] == last; j--);

	for (i = 0; i <= j; i++) 
	{
		StreamPutBits(bs, intra_quant_mate[Table[0][i]], 8);
	}

	if (j < 63)
	{
		StreamPutBits(bs, 0, 8);
	}

	 return;
}

void LoadCustomMpeg4InterMatrix(Stream* bs, const unsigned char* inter_quant_mate)
{
    int i, j;

	const int last = inter_quant_mate[Table[0][63]];

	for (j = 63; j > 0 && inter_quant_mate[Table[0][j - 1]] == last; j--);

	for (i = 0; i <= j; i++) 
	{
		StreamPutBits(bs, inter_quant_mate[Table[0][i]], 8);
	}

	if (j < 63) {
		StreamPutBits(bs, 0, 8);
	}

	 return;
}

int IsCustomIntraMatrix(const short* custom_intra_matrix)
{
    int i = 0;

	while( i < 64)
	{
		if (custom_intra_matrix[i] != mpeg4_intra_matrix[i])
		{
			return 1;
		}
		i++;
	}
	return 0;
}

int IsCustomInterMatrix(const short* custom_inter_matrix)
{
    int i = 0;

	while( i < 64)
	{
		if (custom_inter_matrix[i] != mpeg4_inter_matrix[i])
		{
			return 1;
		}	
		i++;
	}
	return 0;
}

//#pragma CODE_SECTION(Mpeg4IntraQuant1,".internal_code1");
short Mpeg4IntraQuant1(short* Dst, const short* Src, short iQuant, short iScalar, short iFlag)
{
   if (iFlag == 0)
   {
	   const unsigned int quantd = ((4 * iQuant) + (VM18Q / 2)) / VM18Q;
	   const unsigned int mult = multipliers[iQuant];
	   int i;

	   Dst[0] = DIV_DIV(Src[0], (int) iScalar);

		for (i = 1; i < 64; i++) 
		{
			if (Src[i] < 0) 
			{
				unsigned int level = -Src[i];

				level = ((level << 4) + (mpeg4_intra_matrix[i] >> 1)) / mpeg4_intra_matrix[i];
				level = ((level + quantd) * mult) >> SCALEBITS;
				Dst[i] = -(short) level;
			} 
			else if (Src[i] > 0) 
			{
				unsigned int level = Src[i];

				level = ((level << 4) + (mpeg4_intra_matrix[i] >> 1)) / mpeg4_intra_matrix[i];
				level = ((level + quantd) * mult) >> SCALEBITS;
				Dst[i] = level;
			} 
			else 
			{
				Dst[i] = 0;
			}
		}

		return 1;
   }
   else
   {
	   return 1;
   }
}
//***********************************************************************************************
//函 数 名:Mpeg4IntraQuant2
//函数功能:
//形式参数:void
//返 回 值:void
//***********************************************************************************************
#pragma CODE_SECTION(Mpeg4IntraQuant2 ,".internal_code1");
short Mpeg4IntraQuant2(short* Dst, const short* Src, short iQuant, short iScalar)
{
	const unsigned int mult = multipliers[iQuant];
	const unsigned short quant_m_2 = iQuant << 1;
	int i;

	Dst[0] = DIV_DIV(Src[0], (int) iScalar);

	for (i = 1; i < 64; i++)
	{
		short acLevel = Src[i];

		if (acLevel < 0) 
		{
			acLevel = -acLevel;
			if (acLevel < quant_m_2) 
			{
				Dst[i] = 0;
				continue;
			}
			acLevel = (acLevel * mult) >> SCALEBITS;
			Dst[i] = -acLevel;
		} 
		else
		{
			if (acLevel < quant_m_2)
			{
				Dst[i] = 0;
				continue;
			}
			acLevel = (acLevel * mult) >> SCALEBITS;
			Dst[i] = acLevel;
		}
	}

	return(1);

}

//#pragma CODE_SECTION(Mpeg4InterQuant1,".internal_code1");
short Mpeg4InterQuant1(short* Dst, const short* Src, short iQuant, short iFlag)
{
	if (iFlag == 0)
	{
		const unsigned int mult = multipliers[iQuant];
		unsigned int sum = 0;
		int i;

		for (i = 0; i < 64; i++)
		{
			if (Src[i] < 0) 
			{
				unsigned int level = -Src[i];

				level = ((level << 4) + (mpeg4_inter_matrix[i] >> 1)) / mpeg4_inter_matrix[i];
				level = (level * mult) >> 17;
				sum += level;
				Dst[i] = -(short) level;
			}
			else if (Src[i] > 0)
			{
				unsigned int level = Src[i];

				level = ((level << 4) + (mpeg4_inter_matrix[i] >> 1)) / mpeg4_inter_matrix[i];
				level = (level * mult) >> 17;
				sum += level;
				Dst[i] = level;
			}
			else 
			{
				Dst[i] = 0;
			}
		}

		return sum;
	}
	else
	{
       return 0;
	}
}
#pragma CODE_SECTION(Mpeg4InterQuant2,".internal_code1");
short Mpeg4InterQuant2(short* Dst, const short* Src, short iQuant)
{
    const unsigned int mult = multipliers[iQuant];
	const unsigned short quant_m_2 = iQuant << 1;
	const unsigned short quant_d_2 = iQuant >> 1;
	unsigned int sum = 0;
	unsigned int i;

	for (i = 0; i < 64; i++)
	{
		short acLevel = Src[i];

		if (acLevel < 0)
		{
			acLevel = (-acLevel) - quant_d_2;
			if (acLevel < quant_m_2) 
			{
				Dst[i] = 0;
				continue;
			}

			acLevel = (acLevel * mult) >> SCALEBITS;
			sum += acLevel;		/* sum += |acLevel| */
			Dst[i] = -acLevel;
		} 
		else
		{
			acLevel -= quant_d_2;
			if (acLevel < quant_m_2)
			{
				Dst[i] = 0;
				continue;
			}
			acLevel = (acLevel * mult) >> SCALEBITS;
			sum += acLevel;
			Dst[i] = acLevel;
		}
	}

	return(sum);
}



short Mpeg4IntraDeQuant1(short* Dst, const short* Src, short iQuant, short iScalar, short iFlag)
{
   return 1;
}

short Mpeg4InterDeQuant1(short* Dst, const short* Src, short iQuant, short iFlag)
{
	return 1;
}

#pragma CODE_SECTION(Mpeg4IntraDeQuant2, ".internal_code1");
short Mpeg4IntraDeQuant2(short* Dst, const short* Src, short iQuant, short iScalar)
{
    const int quant_m_2 = iQuant << 1;
	const int quant_add = (iQuant & 1 ? iQuant : iQuant - 1);
	int i;

	Dst[0] = Src[0] * iScalar;
	if (Dst[0] < -2048)
	{
		Dst[0] = -2048;
	} 
	else if (Dst[0] > 2047)
	{
		Dst[0] = 2047;
	}

	for (i = 1; i < 64; i++) 
	{
		int acLevel = Src[i];

		if (acLevel == 0)
		{
			Dst[i] = 0;
		} 
		else if (acLevel < 0)
		{
			acLevel = quant_m_2 * -acLevel + quant_add;
			Dst[i] = (acLevel <= 2048 ? -acLevel : -2048);
		}
		else
		{
			acLevel = quant_m_2 * acLevel + quant_add;
			Dst[i] = (acLevel <= 2047 ? acLevel : 2047);
		}
	}

	return(0);
}

#pragma CODE_SECTION(Mpeg4InterDeQuant2,".internal_code1");
short Mpeg4InterDeQuant2(short* Dst, const short* Src, short iQuant)
{
   	const unsigned short quant_m_2 = iQuant << 1;
	const unsigned short quant_add = (iQuant & 1 ? iQuant : iQuant - 1);
	int i;

	for (i = 0; i < 64; i++)
	{
		short acLevel = Src[i];

		if (acLevel == 0) 
		{
			Dst[i] = 0;
		} 
		else if (acLevel < 0) 
		{
			acLevel = acLevel * quant_m_2 - quant_add;
			Dst[i] = (acLevel >= -2048 ? acLevel : -2048);
		} 
		else 
		{
			acLevel = acLevel * quant_m_2 + quant_add;
			Dst[i] = (acLevel <= 2047 ? acLevel : 2047);
		}
	}

	return(0);
}

⌨️ 快捷键说明

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