// AcmCodec.h: interface for the CAcmCodec class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ACMCODEC_H__83295DE1_D443_11D3_8BEA_5254AB148BCA__INCLUDED_)
#define AFX_ACMCODEC_H__83295DE1_D443_11D3_8BEA_5254AB148BCA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define ACM_CODEC_PASS			0	// pass without codec
#define ACM_CODEC_ALAW			1
#define ACM_CODEC_MULAW			2
#define ACM_CODEC_ADPCM			3	// "Microsoft ADPCM";
#define ACM_CODEC_GSM610		4
#define ACM_CODEC_G729CP		5
#define ACM_CODEC_G723_1		6
#define ACM_CODEC_ILBC			7

#define WAVE_FORMAT_GFDVR	WAVE_FORMAT_DEVELOPMENT

class CAcmCodec  
{
public:
	CAcmCodec();
	virtual ~CAcmCodec();

	// ACM_CODEC_XXXX types
	// Set encode or decode flag in bEncode
	// Set the (byte) data length in iLength
	BOOL Init(int iType, BOOL bEncode);	
	BOOL Init(WAVEFORMATEX * pWfx, int iFormatSize, BOOL bEncode);
	void Free();

	// return encoded length, iData is always 16 bits, 8k sampling pcm data
	int Encode(char * pData, char * pCode, int iLength);
	int Decode(char * pData, char * pCode, int iLength);

	// return coded length, if error occurs, return -1
	int GetMaxCodeLength(int iSrcLength);

	inline void SetRawSampleRate(int iSampleRate)	{	m_iRawSampleRate = iSampleRate;	}
	inline void SetRawChannels(int iChannels )		{	m_iRawChannels = iChannels;		}
	WAVEFORMATEX * GetRawFormat()					{	return &m_wfxRaw;				}

protected:
	BOOL SetFormat(int iType);
	void SetRawFormat();
	BOOL NeedConvert(WAVEFORMATEX * pWfx);
	int Convert(unsigned char * pSrc, unsigned char * pDst);
	BOOL PrepareHeader(unsigned char * pSrc, unsigned char * pDst);

	int GetMaxFormatSize();
	BOOL OpenStream(BOOL bEncode);
	BOOL GetStreamSize(int iLength);

protected:
	HACMSTREAM		m_has;  
	ACMSTREAMHEADER m_asHeader;
	WAVEFORMATEX *	m_pwfxCode;
	WAVEFORMATEX	m_wfxRaw;

	DWORD m_dwDstLength;
	DWORD m_dwSrcLength;

	BOOL m_bPass;
	
	int m_iRawSampleRate;
	int m_iRawChannels;

};

#endif // !defined(AFX_ACMCODEC_H__83295DE1_D443_11D3_8BEA_5254AB148BCA__INCLUDED_)
