#include "Page0.h"

#include "p_dsp.h"

extern CCHAR cFile[];
extern CCHAR cTotalFrames[];
extern CCHAR cEncoding[];
extern CCHAR cDecoding[];
extern CCHAR cG729Test[];

#define G729_ENC_INPUT		160
#define G729_DEC_OUTPUT		160

#define	ENC_FILES	3
#define	DEC_FILES	6
#define FILE1_FRAMES	998
#define	FILE2_FRAMES	273
#define	FILE3_FRAMES	800
#define	FILE4_FRAMES	856
#define	FILE5_FRAMES	46
#define	FILE6_FRAMES	116


void G729Init()
{
	DspWriteDMC(PMIN_AGC_USAGE, FALSE);
	DspWriteDMC(PMIN_AEC_USAGE, FALSE);
	DspExecuteAndWait(Dsp_cCodecInit[CODEC_G729]);
	DspWriteDMC(ENC_USE_VAD, TRUE);
}

static CSHORT _sEnc_Frames[ENC_FILES] = {FILE1_FRAMES, FILE2_FRAMES, FILE3_FRAMES};
static CSHORT _sDec_Frames[DEC_FILES] = {FILE1_FRAMES, FILE2_FRAMES, FILE3_FRAMES, FILE4_FRAMES, FILE5_FRAMES, FILE6_FRAMES};

void G729FullTest()
{
	DCHAR iFile;
	DSHORT iFrame;

	DisplayString(cG729Test, 0);
	UdpDebugUpdate();
	ReadFileOpen(FILE_IN_SDRAM_HI);
	SoloFileOpen(FILE_OUT_SDRAM_HI, TRUE, FILE_READ_MAX_SIZE);
	DspDownloadCode(Dsp_cCodeSdramHi[CODEC_G729], Dsp_cCodeSdramLo[CODEC_G729], Dsp_cCodeSize[CODEC_G729]);

	// encode
	for (iFile = 0; iFile < ENC_FILES; iFile ++)
	{
		G729Init();
		DisplayVal(iFile, cFile, 1);
		DisplayVal(_sEnc_Frames[iFile], cTotalFrames, 1);
		DisplayString(cEncoding, 0);
		UdpDebugUpdate();
		SysTickBegin();
		for (iFrame = 0; iFrame < _sEnc_Frames[iFile]; iFrame ++)
		{
			// put the 160 input bytes into from input file to Sdram_pDataCache 
			ReadFile(G729_ENC_INPUT);
			sdram_write(DSP_INPUT_SDRAM_HI, 0, G729_ENC_INPUT/4);

			DspWriteDMC(ENC_INPUT_SDRAM_HI, DSP_INPUT_SDRAM_HI);
//			DspWriteDMC(ENC_INPUT_SDRAM_LO, 0);
			DspWriteDMC(DEC_OUTPUT_SDRAM_LO, 0);
			DspWriteDMC(ENC_OUTPUT_SDRAM_HI, DSP_OUTPUT_SDRAM_HI);
			DspWriteDMC(ENC_OUTPUT_SDRAM_LO, 0);
			DspExecuteAndWait(Dsp_c8KEncode[CODEC_G729]);

			sdram_read(DSP_OUTPUT_SDRAM_HI, 0, G729_COMPRESSED/4);
			// put the 12 output bytes from Sdram_pDataCache to output file
			SoloFileWriteSdramCache(G729_COMPRESSED);
		}
		SysTickEnd();

	}

	// decode
	for (iFile = 0; iFile < DEC_FILES; iFile ++)
	{
		G729Init();
		DisplayVal(iFile, cFile, 1);
		DisplayVal(_sDec_Frames[iFile], cTotalFrames, 1);
		DisplayString(cDecoding, 0);
		UdpDebugUpdate();
		SysTickBegin();

		for (iFrame = 0; iFrame < _sDec_Frames[iFile]; iFrame ++)
		{
			// put the 12 input bytes into from input file to Sdram_pDataCache 
			ReadFile(G729_COMPRESSED);
			sdram_write(DSP_INPUT_SDRAM_HI, 0, G729_COMPRESSED/4);

			DspWriteDMC(DEC_INPUT_SDRAM_HI, DSP_INPUT_SDRAM_HI);
			DspWriteDMC(DEC_INPUT_SDRAM_LO, 0);
			DspWriteDMC(DEC_OUTPUT_SDRAM_HI, DSP_OUTPUT_SDRAM_HI);
			DspWriteDMC(DEC_OUTPUT_SDRAM_LO, 0);
			DspExecuteAndWait(Dsp_c8KDecode[CODEC_G729]);

			sdram_read(DSP_OUTPUT_SDRAM_HI, 0, G729_DEC_OUTPUT/4);
			// put the 160 output bytes from Sdram_pDataCache to output file
			SoloFileWriteSdramCache(G729_DEC_OUTPUT);
		}
		SysTickEnd();
	}

	SoloFileClose();
}

