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

📄 g711.c

📁 基于Tms320C5402的A-law 算法的源代码,该代码已经调试通过,运行结果良好
💻 C
字号:
#include "typedef.h"
#include "g711.h"

/*

FUNCTIONS:

alaw_compress: ... compands 1 vector of linear PCM samples to A-law;
                   uses 13 Most Sig.Bits (MSBs) from input and 8 Least
                   Sig. Bits (LSBs) on output.

alaw_expand: ..... expands 1 vector of A-law samples to linear PCM;
                   use 8 Least Sig. Bits (LSBs) from input and
                   13 Most Sig.Bits (MSBs) on output.
*/

/* ................... Begin of alaw_compress() ..................... */
/*
  ==========================================================================

   FUNCTION NAME: alaw_compress

   DESCRIPTION: ALaw encoding rule according CCITT Rec. G.711.

   PROTOTYPE: void alaw_compress(long lseg, short *linbuf, short *logbuf)

   PARAMETERS:
     lseg:	(In)  number of samples
     linbuf:	(In)  buffer with linear samples (only 12 MSBits are taken
                      into account)
     logbuf:	(Out) buffer with compressed samples (8 bit right justified,
                      without sign extension)

   RETURN VALUE: none.
  ==========================================================================
*/
void alaw_compress(
	Word16 *linbuf,
	Word16 *logbuf,
	Word16 lseg
)
{
	Word16 ix, iexp ;
	Word16 n ;

	for ( n = 0 ; n < lseg ; n ++ )
	{
		ix = linbuf[n] < 0 ? (~linbuf[n]) >> 4 : (linbuf[n]) >> 4;
		/* 0 <= ix < 2048 */
      	/* 1's complement for negative values */

		if (ix > 15)
		{
			iexp = 1;				/* first step: */
			while (ix > 16 + 15)	/* find mantissa and exponent */
			{
				ix >>= 1;
				iexp++;
			}
			ix -= 16;				/* second step: remove leading '1' */
			ix += iexp << 4;		/* now compute encoded value */
		}

		if (linbuf[n] >= 0)
			ix |= (0x0080);			/* add sign bit */
		logbuf[n] = ix ;			/* toggle even bits */
	}

	return ;
}


/*
   FUNCTION NAME: alaw_expand
   DESCRIPTION: ALaw decoding rule according CCITT Rec. G.711.
   PROTOTYPE: void alaw_expand(long lseg, short *logbuf, short *linbuf)
   PARAMETERS:
     lseg:	(In)  number of samples
     logbuf:	(In)  buffer with compressed samples (8 bit right justified,
                      without sign extension)
     linbuf:	(Out) buffer with linear samples (13 bits left justified)

   RETURN VALUE: none.
*/
void alaw_expand(
	Word16 *linbuf,
	Word16 *logbuf,
	Word16 lseg
)
{
	Word16 ix, mant, iexp;
	Word16 n;

	for (n = 0; n < lseg; n++)
	{
		ix = logbuf[n] ;				/* re-toggle toggled bits */
		ix &= (0x007F);					/* remove sign bit */
		iexp = ix >> 4;					/* extract exponent */
		mant = ix & (0x000F);			/* now get mantissa */
		if (iexp > 0)
			mant = mant + 16;			/* add leading '1', if exponent > 0 */

		mant = (mant << 4) + (0x0008);	/* now mantissa left justified and */
		/* 1/2 quantization step added */
		if (iexp > 1)		/* now left shift according exponent */
			mant = mant << (iexp - 1);

		linbuf[n] = logbuf[n] > 127 ? mant : -mant;/* invert, if negative sample */
	}

	return ;
}

⌨️ 快捷键说明

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