#include "../XNetSDKTest.h"
#include "Simplify_Encode.h"
#include "XNetSDKSyn.h"
#include "EncodeCapability.h"
#include "SystemFunction.h"

USE_NS_NETSDK_CFG

#define  IFRAME_EQ_PFRAME 10
#define	 BITMSK(bit)		(int)(1 << (bit))


typedef struct 
{
	unsigned long dwSize;
	unsigned long dsMask;
}SResolution;

enum
{
	MAIN_STREAM = 0,
	EXPAND_STREAM,
	COMBINE_STREAM,
	STREAM_NUM,
};



static SResolution m_resolution[CAPTURE_IMAGE_SIZE_EXT_V3_NR];

static int STREAM_CODE_VALUE[CAPTURE_IMAGE_SIZE_EXT_V3_NR][6] = {
	{512, 768, 1024, 1536, 2048, 2560},	///< D1
	{384, 512, 768, 1024, 1536, 2048},	///< HD1
	{384, 512, 768, 1024, 1536, 2048},	///< BCIF
	{64, 192, 384, 512, 768, 1024},		///< CIF
	{64, 128, 192, 384,  448,  512},	///< QCIF
	{512, 768, 896, 1280, 1536, 2048},	///< VGA
	{256, 384, 512, 640, 768, 1024},	///< QVGA
	{512, 768, 896, 1280, 1536, 2048},	///< SVCD
	{64, 128, 192, 384,  448,  512},	///< QQVGA
	{64, 128, 256, 448, 512, 768},	///< ND1
	{896, 1024, 1536,  2048, 3072, 4096},	///< 650TVL
	{1024,1536, 2048, 2560, 3072, 4096},	///< 720P
	{1024,1536, 2048, 3072, 4096, 5120},	///< 1_3M
	{1024,2048, 3072, 4096, 6144, 8192},	///< UXGA
	{1024,2048, 3072, 4096, 6144, 8192}, ///< 1080P
	{1177,2355, 3532, 4710, 7065, 10240}, ///< WUXGA
	{1280,2560, 3840, 5120, 7680, 10240}, ///< 2_5M
	{1536, 3072, 4096, 6144, 8192, 10240}, ///< 3M
	{2048, 3072, 4096, 6144, 8192,10240}, ///5M
	{1024,1536, 2048, 2560, 3072, 4096},    ///< 1080N
	{1536, 3072, 4096, 6144, 8192, 10240}, ///< 4M
	{1536, 3072, 4096, 6144, 8192, 10240},	///< 6M
	{1536, 3072, 4096, 6144, 8192, 10240},	///< 8M
	{1536, 3072, 4096, 6144, 8192, 10240},	///< 12M
	{2048, 4096, 6144, 8192, 11264, 13312},	///< 4K
	{512, 768, 1024, 1536, 2048, 2560},	///< 720N
	{1024,1536, 2048, 2560, 3072, 4096},	///< WSVGA
	{512, 768, 896, 1280, 1536, 2048},	///< NHD
	{1024,1536, 2048, 3072, 4096, 5120},	///< 3M_N
	{1024,2048, 3072, 4096, 6144, 8192},	///< 4M_N
	{1280, 2560, 3840, 5120, 7680, 10240},	///< 5M_N
	{1536, 3072, 4096, 6144, 8192, 10240},	///< 4K_N
};

//大小眼睛的低码率
static int LOWBITRATE_STREAM_CODE_VALUE[CAPTURE_IMAGE_SIZE_EXT_V3_NR][6] = {
	{384, 576, 768, 1152, 1536, 1920},	///< D1
	{384, 512, 768, 1024, 1536, 2048},	///< HD1
	{384, 512, 768, 1024, 1536, 2048},	///< BCIF
	{48, 144, 288, 384, 576, 768},	///< CIF
	{48, 96, 144, 288, 336, 384},	///< QCIF
	{512, 768, 896, 1280, 1536, 2048},	///< VGA
	{256, 384, 512, 640, 768, 1024},	///< QVGA
	{512, 768, 896, 1280, 1536, 2048},	///< SVCD
	{64, 128, 192, 384,  448,  512},	///< QQVGA
	{64, 128, 256, 448, 512, 768},		///< ND1
	{896, 1024, 1536,  2048, 3072, 4096},	///< 960H
	{768, 1152, 1536, 1920, 2304, 3072},	///< 720P
	{1024,1536, 2048, 3072, 4096, 5120},	///< 960P
	{1024,2048, 3072, 4096, 6144, 8192},	///< UXGA
	{1024,2048, 3072, 4096, 6144, 8192},	///< 1080P
	{640, 1024, 1536, 2048, 3072, 4096},	///< WUXGA
	{640, 1024, 1536, 2048, 3072, 4096},	///< 2_5M
	{640, 1024, 1536, 2048, 3072, 4096},	///< 3M
	{720, 1024, 1536, 2048, 3072, 5120},	///< 5M
	{0,0, 0, 0, 0, 0},    ///< 1080N
	{0, 0, 0, 0, 0, 0}, ///< 4M
	{0,0,0,0,0,0}, //6M
	{0,0,0,0,0,0}, //8M
	{0,0,0,0,0,0}, //12M
	{0,0,0,0,0,0}, //4K
	{0, 0, 0, 0, 0, 0},	///< 720N
	{0,0,0,0,0,0}, //RES1
	{0,0,0,0,0,0}, //RES2
	{0,0,0,0,0,0}, //RES3
	{0,0,0,0,0,0}, //RES4
	{0,0,0,0,0,0}, //RES5
	{0,0,0,0,0,0}, //RES6	
};

static int FISHEYE_STREAM_CODE_VALUE[CAPTURE_IMAGE_SIZE_EXT_V3_NR][6] = {

	{512, 768, 1024, 1536, 2048, 2560},	///< D1
	{384, 512, 768, 1024, 1536, 2048},	///< HD1
	{384, 512, 768, 1024, 1536, 2048},	///< BCIF
	{64, 192, 384, 512, 768, 1024},		///< CIF
	{64, 128, 192, 384,  448,  512},	///< QCIF
	{512, 768, 896, 1280, 1536, 2048},	///< VGA
	{256, 384, 512, 640, 768, 1024},	///< QVGA
	{512, 768, 896, 1280, 1536, 2048},	///< SVCD
	{64, 128, 192, 384,  448,  512},	///< QQVGA
	{64, 128, 256, 448, 512, 768},	///< ND1
	{896, 1024, 1536,  2048, 3072, 4096},	///< 650TVL
	{1536, 2048, 2560, 3072, 4096, 6096},	///< 720P
	{1024,1536, 2048, 3072, 4096, 5120},	///< 1_3M
	{1024,2048, 3072, 4096, 6144, 8192},	///< UXGA
	{1024,2048, 3072, 4096, 6144, 8192}, ///< 1080P
	{1177,2355, 3532, 4710, 7065, 10240}, ///< WUXGA
	{1280,2560, 3840, 5120, 7680, 10240}, ///< 2_5M
	{1536, 3072, 4096, 6144, 8192, 10240}, ///< 3M
	{2048, 3072, 4096, 6144, 8192,10240}, ///5M
	{1024,1536, 2048, 2560, 3072, 4096},    ///< 1080N
	{1536, 3072, 4096, 6144, 8192, 10240}, ///< 4M
	{0,0,0,0,0,0}, //6M
	{0,0,0,0,0,0}, //8M
	{0,0,0,0,0,0}, //12M
	{0,0,0,0,0,0}, //4K
	{512, 768, 1024, 1536, 2048, 2560},	///< 720N
	{1024,1536, 2048, 2560, 3072, 4096},	///< WSVGA
	{0,0,0,0,0,0}, //RES2
	{0,0,0,0,0,0}, //RES3
	{0,0,0,0,0,0}, //RES4
	{0,0,0,0,0,0}, //RES5
	{0,0,0,0,0,0}, //RES6	
};
static int H265_STREAM_CODE_VALUE[CAPTURE_IMAGE_SIZE_EXT_V3_NR][6] = {

	{332, 499, 665, 998, 1331, 1664},	///< D1
	{249, 332, 499, 665, 998, 1331},	///< HD1
	{249, 332, 499, 665, 998, 1331},	///< BCIF
	{41, 124, 249, 332, 499, 665},		///< CIF
	{41, 83, 124, 249,  291,  332},	///< QCIF
	{332, 499, 582, 832, 998, 1331},	///< VGA
	{166, 249, 332, 416, 499, 665},	///< QVGA
	{332, 499, 582, 832, 998, 1331},	///< SVCD
	{41, 83, 124, 249,  291,  332},	///< QQVGA
	{41, 83, 166, 291, 332, 499},	///< ND1
	{582, 665, 998,  1331, 1996, 2662},	///< 650TVL
	{665,998, 1331, 1664, 1996, 2662},	///< 720P
	{665,998, 1331, 1996, 2662, 3328},	///< 1_3M
	{665,1331, 1996, 2662, 3993, 5324},	///< UXGA
	{665,1331, 1996, 2662, 3993, 5324}, ///< 1080P
	{765,1530, 2295, 3061, 4592, 6656}, ///< WUXGA
	{832,1664, 2496, 3328, 4992, 6656}, ///< 2_5M
	{998, 1996, 2662, 3993, 5324, 6656}, ///< 3M
	{1331, 1996, 2662, 3993, 5324,6656}, ///5M
	{665,998, 1331, 1664, 1996, 2662},    ///< 1080N
	{998, 1996, 2662, 3993, 5324, 6656}, ///< 4M
	{998, 1996, 2662, 3993, 5324, 6656},	///< 6M
	{998, 1996, 2662, 3993, 5324, 6656},	///< 8M
	{998, 1996, 2662, 3993, 5324, 6656},	///< 12M
	{1331, 2662, 3993, 5324, 7321, 8652},	///< 4K
	{332, 499, 665, 998, 1331, 1664},	///< 720N
	{582, 665, 998,  1331, 1996, 2662},		///< WSVGA(1024*576)
	{332, 499, 582, 832, 998, 1327},		///< NHD
	{665,998, 1331, 1996, 2662, 3328},		///< 3M_N
	{665,1331, 1996, 2662, 3993, 5324},		///< 4M_N
	{832, 1664, 2496, 3328, 4992, 6656},	///< 5M_N
	{1331, 1996, 2665, 3993, 5324, 6656},	///< 4K_N
};


//函数说明：计算码固定码率支持的码流值
//参数说明：
//dstFormat		: 媒体格式
//bLowBitrate	: 是否支持大小眼
//bLowBitrate	: 是否支持鱼眼
//bH265Bitrate	: 是否支持h265

void CalculateCodeStreamValue(SDK_VIDEO_FORMAT * vfSetFormat, bool bLowBitrate, bool bFisheyeBitrate, bool bH265Bitrate)
{
	int j = 0; 
	int nTmpFPS = 0;
	int nFPS = 25;
	nTmpFPS = min(vfSetFormat->nFPS, nFPS);
	int iBitrate = 0;

	if(bLowBitrate)
	{
		for (j = 0; j < sizeof(LOWBITRATE_STREAM_CODE_VALUE[vfSetFormat->iResolution]) / sizeof(int); ++j)
		{
			iBitrate = LOWBITRATE_STREAM_CODE_VALUE[vfSetFormat->iResolution][j] * (vfSetFormat->iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (vfSetFormat->iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
			
			printf("Support iBitrate[%d] = %d\r\n", j, iBitrate);
		}
	}
	else if(bFisheyeBitrate)
	{
		for (j = 0; j < sizeof(FISHEYE_STREAM_CODE_VALUE[vfSetFormat->iResolution]) / sizeof(int); ++j)
		{
			iBitrate = FISHEYE_STREAM_CODE_VALUE[vfSetFormat->iResolution][j] * (vfSetFormat->iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (vfSetFormat->iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
			printf("Support iBitrate[%d] = %d\r\n", j, iBitrate);
		}
	}
	else if(bH265Bitrate)
	{
		for (j = 0; j < sizeof(H265_STREAM_CODE_VALUE[vfSetFormat->iResolution]) / sizeof(int); ++j)
		{
			iBitrate = H265_STREAM_CODE_VALUE[vfSetFormat->iResolution][j] * (vfSetFormat->iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (vfSetFormat->iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
			printf("Support iBitrate[%d] = %d\r\n", j, iBitrate);
		}
	}
	else
	{
		for (j = 0; j < sizeof(STREAM_CODE_VALUE[vfSetFormat->iResolution]) / sizeof(int); ++j)
		{
			iBitrate = STREAM_CODE_VALUE[vfSetFormat->iResolution][j] * (vfSetFormat->iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (vfSetFormat->iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
			printf("Support iBitrate[%d] = %d\r\n", j, iBitrate);
		}
	}
}

void GetMaxResolAndRate(int &nResolu, int &nRate, long lFreePower)
{
	if ( lFreePower <= 0 )
	{
		return;
	}

	int nSize = m_resolution[nResolu].dwSize;
	if ( nSize > lFreePower )
	{
		nResolu = -1;
		nRate = -1;
		return;
	}
	else
	{
		int nTempRate = lFreePower / nSize;

		if ( nTempRate > 25 )
		{
			nTempRate = 25;
		}

		nRate = nTempRate;
	}

	long lMaxSize = nSize;
	for ( int i = 0; i < CAPTURE_IMAGE_SIZE_EXT_V3_NR; i ++ )
	{
		long lTempsize = m_resolution[i].dwSize;
		if ( lFreePower >= lTempsize && lMaxSize < lTempsize )
		{
			nResolu = i;
			lMaxSize = lTempsize;
		}
	}
}

void GetSupportRes(int maxSize, unsigned long dwMask )
{
	for (int i = 0; i < CAPTURE_IMAGE_SIZE_EXT_V3_NR ; i ++)
	{
		if ( (m_resolution[i].dwSize <= m_resolution[maxSize].dwSize ) 
			&& ( dwMask & (0x01<<i) ) )
		{
			printf("m_resolution[%d]\r\n", i);
		}
	}	
}

int TestEncodeConfig()
{
	printf("%s\r\n", __FUNCTION__);
	char c = 0;
	bool bSupport = false;
	char szOutBuffer[40960] = {0};
	int  nInOutSize = sizeof(szOutBuffer);
	int nResult = 0;
	int nCompression;
	
	int nMainStreamResolution = 0;
//	unsigned long ulMask = 0;
	int nQuality = 0;
	int nGop = 0;
	int nFps = 0;

	bool m_bH265Bitrate[STREAM_NUM];

	bool m_bMultiStream = false;
	bool m_bLowBitrate = false;
	bool m_bFisheyeBitrate = false;
	m_bH265Bitrate[MAIN_STREAM] = false;
	m_bH265Bitrate[EXPAND_STREAM] = false;
	m_bH265Bitrate[COMBINE_STREAM] = false;

	unsigned long lFreePower = 0;

	///////////////////////////////////现有支持的所有分辨率/////////////////////////////////
	m_resolution[CAPTURE_IMAGE_SIZE_D1].dwSize = 704 * 480;
	m_resolution[CAPTURE_IMAGE_SIZE_HD1].dwSize = 704 * 240;
	m_resolution[CAPTURE_IMAGE_SIZE_BCIF].dwSize = 352 * 480;
	m_resolution[CAPTURE_IMAGE_SIZE_CIF].dwSize = 352 * 240;
	m_resolution[CAPTURE_IMAGE_SIZE_QCIF].dwSize = 176 * 120;
	m_resolution[CAPTURE_IMAGE_SIZE_VGA].dwSize = 640 * 480;
	m_resolution[CAPTURE_IMAGE_SIZE_QVGA].dwSize = 320 * 240;
	m_resolution[CAPTURE_IMAGE_SIZE_SVCD].dwSize = 480 * 480;
	m_resolution[CAPTURE_IMAGE_SIZE_QQVGA].dwSize = 160 * 128;

	m_resolution[CAPTURE_IMAGE_SIZE_ND1].dwSize = 240*192;
	m_resolution[CAPTURE_IMAGE_SIZE_650TVL].dwSize = 928*480;
	m_resolution[CAPTURE_IMAGE_SIZE_720P].dwSize = 1280*720;
	m_resolution[CAPTURE_IMAGE_SIZE_1_3M].dwSize = 1280*960;
	m_resolution[CAPTURE_IMAGE_SIZE_UXGA].dwSize = 1600*1200;
	m_resolution[CAPTURE_IMAGE_SIZE_1080P].dwSize = 1920*1080;
	m_resolution[CAPTURE_IMAGE_SIZE_WUXGA].dwSize = 1920*1200;
	m_resolution[CAPTURE_IMAGE_SIZE_2_5M].dwSize = 1872*1408;
	m_resolution[CAPTURE_IMAGE_SIZE_3M].dwSize = 2048*1536;
	m_resolution[CAPTURE_IMAGE_SIZE_5M].dwSize = 3744*1408;
	m_resolution[CAPTURE_IMAGE_SIZE_1080N].dwSize = 944*1080; 
	m_resolution[CAPTURE_IMAGE_SIZE_4M].dwSize = 2592*1520; 
	m_resolution[CAPTURE_IMAGE_SIZE_6M].dwSize = 3072*2048; 
	m_resolution[CAPTURE_IMAGE_SIZE_8M].dwSize = 3264*2448; 
	m_resolution[CAPTURE_IMAGE_SIZE_12M].dwSize = 4000*3000; 
	m_resolution[CAPTURE_IMAGE_SIZE_4K].dwSize = 4096*2160; 
	m_resolution[CAPTURE_IMAGE_SIZE_720N].dwSize = 640*720; 
	m_resolution[CAPTURE_IMAGE_SIZE_WSVGA].dwSize = 1024*576; 
	m_resolution[CAPTURE_IMAGE_SIZE_NHD].dwSize = 640*360; 
	m_resolution[CAPTURE_IMAGE_SIZE_3M_N].dwSize = 1024*1536; 
	m_resolution[CAPTURE_IMAGE_SIZE_4M_N].dwSize = 1296*1520; 
	m_resolution[CAPTURE_IMAGE_SIZE_5M_N].dwSize = 1872*1408; 
	m_resolution[CAPTURE_IMAGE_SIZE_4K_N].dwSize = 2048*2160;  ///2048 * 2160通用/1920*2160海思

	/////////////////////////////////////////////掩码//////////////////////////////////////////
	m_resolution[CAPTURE_IMAGE_SIZE_D1].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_HD1].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_BCIF].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_BCIF) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_CIF].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_QCIF].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_VGA].dsMask =  BITMSK(CAPTURE_IMAGE_SIZE_VGA) | BITMSK(CAPTURE_IMAGE_SIZE_QVGA);
	m_resolution[CAPTURE_IMAGE_SIZE_QVGA].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_QVGA);
	m_resolution[CAPTURE_IMAGE_SIZE_SVCD].dsMask =  BITMSK(CAPTURE_IMAGE_SIZE_SVCD);
	m_resolution[CAPTURE_IMAGE_SIZE_QQVGA].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_QQVGA);
	m_resolution[CAPTURE_IMAGE_SIZE_ND1].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_ND1);
	
	m_resolution[CAPTURE_IMAGE_SIZE_650TVL].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_720P].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_1_3M].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_UXGA].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_1080P].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_WUXGA].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_2_5M].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_3M].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_5M].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);
	m_resolution[CAPTURE_IMAGE_SIZE_1080N].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_650TVL) | BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF) | BITMSK(CAPTURE_IMAGE_SIZE_720P); 
	m_resolution[CAPTURE_IMAGE_SIZE_4M].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_D1) | BITMSK(CAPTURE_IMAGE_SIZE_HD1) | BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF) | BITMSK(CAPTURE_IMAGE_SIZE_720P); 
	m_resolution[CAPTURE_IMAGE_SIZE_720N].dsMask = BITMSK(CAPTURE_IMAGE_SIZE_CIF) | BITMSK(CAPTURE_IMAGE_SIZE_QCIF);

	while((c = getchar()) != 'q')
	{
		switch(c)
		{
			//判断是否支持组合编码
		case 'a':
			{
				memset(&szOutBuffer, 0, 40960);
				nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_SystemFunction, szOutBuffer, &nInOutSize, 4000, JK_SystemFunction_MsgId);

				if(nResult >= 0)
				{
					SystemFunction sysfunc;
					sysfunc.Parse(szOutBuffer);

					bSupport = sysfunc.mEncodeFunction.CombineStream.ToBool();
					m_bMultiStream = sysfunc.mEncodeFunction.DoubleStream.ToBool();
					m_bLowBitrate =  sysfunc.mEncodeFunction.LowBitRate.ToBool();   
					m_bFisheyeBitrate = sysfunc.mOtherFunction.SupportFishEye.ToBool();

					printf("bSupport = %d, m_bMultiStream = %d, m_bLowBitrate = %d, m_bFisheyeBitrate = %d\r\n", 
							bSupport, m_bMultiStream, m_bLowBitrate, m_bFisheyeBitrate);
				}
			}
			break;
		case 'b':
			{
				memset(&szOutBuffer, 0, 40960);
				nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_EncodeCapability, szOutBuffer, &nInOutSize, 4000, EXCMD_ABILITY_GET);

				if(nResult >= 0)
				{
					EncodeCapability encode;
					encode.Parse(szOutBuffer);

					///////////////////////////////////获取主辅码流支持的压缩格式/////////////////////////////////
					////获取主码流支持的压缩格式
					nCompression = encode.mEncodeInfo[0].CompressionMask.Value();

					for ( int i = 0; i < CAPTURE_COMPRESS_NR; i ++ )
					{
						if (nCompression & ( 1 << i ) )
						{
							printf("[Main]Support Compression Type = %s\r\n", CompressionMap[i].name);
						}
					}

					////获取辅码流的支持的压缩格式
					nCompression = encode.mEncodeInfo[1].CompressionMask.Value();

					for ( int i = 0; i < CAPTURE_COMPRESS_NR; i ++ )
					{
						if (nCompression & ( 1 << i ) )
						{
							printf("[Extra]Support Compression Type = %s\r\n", CompressionMap[i].name);
						}
					}

					lFreePower = encode.MaxEncodePower.Value();
					//ulMask = encode.ImageSizePerChannel[0].Value();

				}
			}
			break;
		case 'g':
			{
				char szOutBuf[40960] = {0};
				int nInOutBufLen = sizeof(szOutBuf);
				SDK_VIDEO_FORMAT mainFormat;
				memset(&mainFormat, 0, sizeof(SDK_VIDEO_FORMAT));

				int nGetCfg = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Simplify_Encode, szOutBuf, &nInOutBufLen, 4000, EXCMD_CONFIG_GET);
				if (nGetCfg >= 0 && nInOutBufLen > 0)
				{
					JObjArrayObject<Simplify_Encode> cfg(NULL, JK_Simplify_Encode);
					cfg.Parse(szOutBuf);
					if(cfg.objs.Size() > 0)
					{
						///主码流分辨率
						const char * mainStrRes = cfg.objs[0].mMainFormat.mVideo.Resolution.Value();
						printf("MainFormat[Resolution:%s]\r\n", cfg.objs[0].mMainFormat.mVideo.Resolution.Value());

						for ( int i = 0; i < CAPTURE_IMAGE_SIZE_EXT_V3_NR; i ++ )
						{
							if(strcmp(mainStrRes, s_videoResolutionMap[i].name) == 0)
							{
								nMainStreamResolution = i;
							}
						}

						if(nMainStreamResolution == CAPTURE_COMPRESS_H265)
						{
							m_bH265Bitrate[EXPAND_STREAM] = true;
						}
						else
						{
							m_bH265Bitrate[EXPAND_STREAM] = false;
						}

						mainFormat.iResolution = nMainStreamResolution;

						///主码流帧率
						nFps = cfg.objs[0].mMainFormat.mVideo.FPS.Value();
						printf("MainFormat[FPS:%d]\r\n", cfg.objs[0].mMainFormat.mVideo.FPS.Value());
						
						mainFormat.nFPS = nFps;

						///主码流码流值
						printf("MainFormat[BitRate:%d]\r\n", cfg.objs[0].mMainFormat.mVideo.BitRate.Value());

						mainFormat.nBitRate = cfg.objs[0].mMainFormat.mVideo.BitRate.Value();

						///主码流帧间隔
						nGop = cfg.objs[0].mMainFormat.mVideo.GOP.Value();
						printf("MainFormat[GOP:%d]\r\n", cfg.objs[0].mMainFormat.mVideo.GOP.Value());

						mainFormat.iGOP = cfg.objs[0].mMainFormat.mVideo.GOP.Value();

						///主码流码率控制
						printf("MainFormat[BitRateControl:%s]\r\n", cfg.objs[0].mMainFormat.mVideo.BitRateControl.Value());


						///主码流压缩格式
						printf("MainFormat[Compression:%s]\r\n", cfg.objs[0].mMainFormat.mVideo.Compression.Value());

						int iCompression = 0;
						for ( int i = 0; i < CAPTURE_COMPRESS_NR; i ++ )
						{
							if(strcmp(mainStrRes, CompressionMap[i].name) == 0)
							{
								iCompression = i;
							}
						}

						mainFormat.iCompression = iCompression;

						///主码流画质
						nQuality = cfg.objs[0].mMainFormat.mVideo.Quality.Value();
						printf("MainFormat[Quality:%d]\r\n", cfg.objs[0].mMainFormat.mVideo.Quality.Value());

						mainFormat.iQuality = nQuality;


						/////////////////////////////////////////////设置//////////////////////////////////////////////////
						int nChannelCount = 1;				//通道个数，默认为1
						unsigned long dwChnUsed = 0;
						unsigned long dwChnFree = 0;
						unsigned long dwPower = lFreePower / nChannelCount;
						int nExStreamResolution = 0;
						
						int nExFps = 0;

						const char * ExStrRes = cfg.objs[0].mExtraFormat.mVideo.Resolution.Value();
						nExFps = cfg.objs[0].mExtraFormat.mVideo.FPS.Value();

						for ( int i = 0; i < CAPTURE_IMAGE_SIZE_EXT_V3_NR; i ++ )
						{
							if(strcmp(ExStrRes, s_videoResolutionMap[i].name) == 0)
							{
								nExStreamResolution = i;
							}
						}

						if (m_bMultiStream && cfg.objs[0].mExtraFormat.VideoEnable.Value())
						{
							dwChnUsed = m_resolution[nExStreamResolution].dwSize * nExFps;
						}
						dwChnFree = dwPower - dwChnUsed;
	
						////////////主码流/////////
						mainFormat.nFPS = 25;
						int MaxSize = 0;
						GetMaxResolAndRate(MaxSize, nFps, dwChnFree);
						//GetSupportRes(MaxSize, ulMask);

						//////固定码率时的码流值选择
						CalculateCodeStreamValue(&mainFormat, m_bLowBitrate, m_bFisheyeBitrate, m_bH265Bitrate);

						//////////设置码流值
						cfg.objs[0].mMainFormat.mVideo.FPS.SetValue(25);
						mainFormat.nFPS = 25;
						int nFPS = 25;
						int nTmpFPS = 25;		/////设置的帧率
						int dwBitrate = 0;

						//////////码流值的计算////////
						if (m_bLowBitrate)
						{		
							dwBitrate = LOWBITRATE_STREAM_CODE_VALUE[mainFormat.iResolution][mainFormat.iQuality - 1] * (mainFormat.iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (mainFormat.iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
						}
						else if(m_bFisheyeBitrate)
						{
							dwBitrate = FISHEYE_STREAM_CODE_VALUE[mainFormat.iResolution][mainFormat.iQuality - 1] * (mainFormat.iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (mainFormat.iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
						}
						else if (m_bH265Bitrate[MAIN_STREAM])
						{
							dwBitrate = H265_STREAM_CODE_VALUE[mainFormat.iResolution][mainFormat.iQuality - 1] * (mainFormat.iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (mainFormat.iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
						}
						else
						{
							dwBitrate = STREAM_CODE_VALUE[mainFormat.iResolution][mainFormat.iQuality - 1] * (mainFormat.iGOP * nTmpFPS +  IFRAME_EQ_PFRAME - 1) / (mainFormat.iGOP * nFPS + IFRAME_EQ_PFRAME - 1);
						}

						cfg.objs[0].mMainFormat.mVideo.BitRate.SetValue(dwBitrate);
						const char *pCfg = cfg.ToString();

						XSDK_DevSetSysConfig(g_hDevice, JK_Simplify_Encode, pCfg, strlen(pCfg), 1, 5000, EXCMD_CONFIG_SET);
					}
				}
			}
			break;
		
		}
	}
	return 0;
}


