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

📄 g729codec.cxx

📁 rtp在linux下的实现
💻 CXX
字号:
// G729Codec.cpp: implementation of the G729Codec class.
//
//////////////////////////////////////////////////////////////////////

#include "G729Codec.h"
#include "Winsock.h"

extern "C" {
	#include "G729A\typedef.h"
	#include "G729A\ld8a.h"
	#include "G729A\bits.c"

	extern FLOAT *new_speech;           /* Pointer to new speech data   */
}

// encoder
static int prm[PRM_SIZE];				/* Transmitted parameters        */
static INT16 serial[SERIAL_SIZE];		/* Output bit stream buffer      */
static INT16 sp16[L_FRAME];				/* Buffer to read 16 bits speech */

// decoder
static FLOAT  synth_buf[L_FRAME+M];     /* Synthesis                  */
FLOAT  *synth;
static int parm[PRM_SIZE+2];            /* Synthesis parameters + BFI */
static INT16 serialD[SERIAL_SIZE];      /* Serial stream              */
static FLOAT  Az_dec[MP1*2];            /* Decoded Az for post-filter */
static int T2[2];                       /* Decoded Pitch              */
extern "C" {
	int bad_lsf;						/* bad LSF indicator   */
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

G729Codec::G729Codec()
{

}

G729Codec::~G729Codec()
{

}

int 
G729Codec::StartEncoder()
{
	init_pre_process();
	init_coder_ld8a();

	return 0;
}

int
G729Codec::StopEncoder()
{
	return 0;
}

int
G729Codec::StartDecoder()
{
	for (int i=0; i<M; i++) synth_buf[i] = (F)0.0;
	synth = synth_buf + M;

	bad_lsf = 0;          /* Initialize bad LSF indicator */
	init_decod_ld8a();
	init_post_filter();
	init_post_process();

	return 0;
}

int
G729Codec::StopDecoder()
{
	return 0;
}

int
G729Codec::Encode(void *input, int inputSizeBytes, void *output, int *outputSizeBytes)
{
	char *dataIn = (char *)input;
	char *dataOut = (char *)output;

	if (inputSizeBytes > 0) {
		if (inputSizeBytes > *outputSizeBytes * 16) 
		{
			*outputSizeBytes = 0;
			return -10;
		}
		else if (inputSizeBytes % 160 != 0) {	// input must be multiple of 10ms (160 bytes of pcm 16bit 8kHz Mono)
			*outputSizeBytes = 0;
			return -20;
		}
		else 
		{
			// convert each frame in input
			const int nFrames = inputSizeBytes / 160;
			for (int i = 0; i < nFrames; i++) 
			{
				char  * g729Data = (char  *)&dataOut[10 * i];
				char *  pcmData = (char *)&dataIn[160 * i];

				// itu
				INT16 *pcmData16 = (INT16 *)pcmData;
				INT16 *g729Data16 = (INT16 *)g729Data;
				
				for (int j = 0; j < L_FRAME; j++)  new_speech[j] = (FLOAT) pcmData16[j];
				pre_process( new_speech, L_FRAME);
				coder_ld8a(prm);
				prm2bits_ld8k(prm, serial);
				for (j=0; j<5; j++)	// 10 bytes = 5 16-bit-words
				{
					short s1 = bin2int(16, &(serial[2+16*j]));
					short s2 = ntohs(s1);
					g729Data16[j] = s2;
				}
			}
			*outputSizeBytes = nFrames * 10;
		}
	}
	return 0;
}

int
G729Codec::Decode(void *input, int inputSizeBytes, void *output, int *outputSizeBytes)
{
	char *dataIn = (char *)input;
	char *dataOut = (char *)output;

	if (inputSizeBytes * 16 > *outputSizeBytes) 
	{
		*outputSizeBytes = 0;
		return -10;
	}
	else if (inputSizeBytes % 10 != 0) 
	{	// input must be multiple of 10ms (10 bytes of G729)
		*outputSizeBytes = 0;
		return -20;
	}
	else 
	{
		const int nFrames = inputSizeBytes / 10;
		for (int i = 0; i < nFrames; i++)
		{
			short *  pcmData = (short *)&dataOut[160 * i];
			char  * g729Data = (char  *)&dataIn[ 10 * i];
			INT16 * g729Data16 = (INT16*)g729Data;

			for (int j=0; j<5; j++)
			{
				INT16 v1 = g729Data16[j];
				INT16 v2 = ntohs(v1);
				int2bin(v2, 16, &serialD[2+16*j]);
			}

			bits2prm_ld8k( &serialD[2], &parm[1]);

			parm[0] = 0;           /* No frame erasure */

			parm[4] = check_parity_pitch(parm[3], parm[4] ); /* get parity check result */

			decod_ld8a(parm, synth, Az_dec, T2);             /* decoder */

			post_filter(synth, Az_dec, T2);                  /* Post-filter */

			post_process(synth, L_FRAME);                    /* Highpass filter */

			for (j=0; j<L_FRAME; j++)
			{
				pcmData[j] = (INT16)synth[j];
			}
		}
		*outputSizeBytes = nFrames * 160;
	}
	return 0;
}

⌨️ 快捷键说明

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