#include "Page0.h"

#include "p_dsp.h"

//#define G723_ENC_INPUT		480
#define G723_ENC_INPUT1		256
#define G723_ENC_INPUT2		224
#define G723_DEC_OUTPUT1	256
#define G723_DEC_OUTPUT2	224


#define	ENC_FILES	2
#define	DEC_FILES	4

#define FILE1_FRAMES	864
#define FILE2_FRAMES	120
#define FILE3_FRAMES	864
#define FILE4_FRAMES	120
#define FILE5_FRAMES	120
#define FILE6_FRAMES	11

#define FILE1_VAD  1
#define FILE1_RATE 1
#define FILE2_VAD  1
#define FILE2_RATE 0

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

static CCHAR  _cEnc_Vad[ENC_FILES] = {FILE1_VAD, FILE2_VAD};
static CCHAR  _cEnc_Rate[ENC_FILES] = {FILE1_RATE, FILE2_RATE};
static CSHORT _sEnc_Frames[ENC_FILES] = {FILE1_FRAMES, FILE2_FRAMES};
static CSHORT _sDec_Frames[DEC_FILES] = {FILE3_FRAMES, FILE4_FRAMES, FILE5_FRAMES, FILE6_FRAMES};

void G723Init(UCHAR iIndex)
{
	DCHAR iFile;

	iFile = iIndex;
	DspWriteDMC(PMIN_AGC_USAGE, FALSE);
	DspWriteDMC(PMIN_AEC_USAGE, FALSE);
	DspExecuteAndWait(Dsp_cCodecInit[CODEC_G7231]);

// Set the VAD and Bit Rate options
	DspWriteDMC(ENC_USE_VAD, _cEnc_Vad[iFile]);
	DspWriteDMC(ENC_USE_HIGH_RATE, _cEnc_Rate[iFile]);
}

void G723FullTest()
{
	DCHAR iFile;
	DSHORT iFrame;

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

	// encode
	for (iFile = 0; iFile < ENC_FILES; iFile ++)
	{
		G723Init(iFile);
		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 480 input bytes into from input file to Sdram_pDataCache 
			ReadFile(G723_ENC_INPUT1);
			sdram_write(DSP_INPUT_SDRAM_HI, 0, G723_ENC_INPUT1/4);
			ReadFile(G723_ENC_INPUT2);
			sdram_write(DSP_INPUT_SDRAM_HI, 1, G723_ENC_INPUT2/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_G7231]);

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

	}

	// decode
	for (iFile = 0; iFile < DEC_FILES; iFile ++)
	{
		G723Init(iFile);
		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(G7231_COMPRESSED);
			sdram_write(DSP_INPUT_SDRAM_HI, 0, G7231_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_G7231]);

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

	SoloFileClose();
}

