[36]_海思SDK编码部分之sample_venc.c详解

不多说了,直接上源码+分析......

/******************************************************************************
  A simple program of Hisilicon HI3531 video encode implementation.
  Copyright (C), 2010-2011, Hisilicon Tech. Co., Ltd.
 ******************************************************************************
    Modification:  2011-2 Created
******************************************************************************/
#ifdef __cplusplus 	//__cplusplus是C++中定义的一个宏,如果这个宏被定义,说明这个程序是C++程序
#if __cplusplus 	//那么如果C++程序要调用C实现的函数或库等,则需要使用extern "C"{ 
extern "C"{		//这是实现C++调用C库或函数的一种手段,原因是因为C++和C语言的编译器不同导致
#endif
#endif /* End of #ifdef __cplusplus */
 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include "sample_comm.h"
 
//VIDEO_NORM_E gs_enNorm = VIDEO_ENCODING_MODE_PAL; //PAL(德国制@25帧),中国主要也是使用这种。
VIDEO_NORM_E gs_enNorm = VIDEO_ENCODING_MODE_NTSC; //NTSC(美国制@30帧),(来自网络)更换N制的镜头后,VI VO的属性都要设置成N制的
//VIDEO_NORM_E gs_enNorm = VIDEO_ENCODING_MODE_AUTO, 
//VIDEO_NORM_E gs_enNorm = VIDEO_ENCODING_MODE_BUTT;
 
 
#ifdef hi3518ev201
 
HI_U32 g_u32BlkCnt = 4;
#endif
 
#ifdef hi3518ev200
 
HI_U32 g_u32BlkCnt = 4;  //啥意思?块大小?
 
#endif
 
#ifdef hi3516cv200
 
HI_U32 g_u32BlkCnt = 10;
 
#endif
 
																  //2018.06.02
/******************************************************************************
* function : show usage  			                   介绍使用方法 
******************************************************************************/
void SAMPLE_VENC_Usage(char *sPrgNm)  //接收的字符串为argv[0]
{
    printf("Usage : %s \n", sPrgNm); //string program number
    printf("index:\n");
    printf("\t 0) 1*1080p H264 + 1*VGA H264.\n");
    printf("\t 1) 1*1080p MJPEG encode + 1*1080p jpeg.\n");
    printf("\t 2) low delay encode(only vi-vpss online).\n");
    printf("\t 3) roi background framerate.\n");
    printf("\t 4) Thumbnail of 1*1080p jpeg.\n");
#ifndef hi3518ev201
	printf("\t 5) svc-t H264\n");
#endif
    return;
}
																  //2018.06.02
/******************************************************************************
* function : to process abnormal case         处理异常情况,譬如中断处理、结束进程                                      
******************************************************************************/
void SAMPLE_VENC_HandleSig(HI_S32 signo)
{
    if (SIGINT == signo || SIGTERM == signo)
    {
        SAMPLE_COMM_ISP_Stop();
        SAMPLE_COMM_SYS_Exit();
        printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
    }
    exit(-1);
}
																  //2018.06.02
/******************************************************************************
* function : to process abnormal case - the case of stream venc     编码异常处理
******************************************************************************/
void SAMPLE_VENC_StreamHandleSig(HI_S32 signo)
{
 
    if (SIGINT == signo || SIGTSTP == signo)
    {
        SAMPLE_COMM_ISP_Stop();
        SAMPLE_COMM_SYS_Exit();
        printf("\033[0;31mprogram exit abnormally!\033[0;39m\n");
    }
 
    exit(0);
}
 
																  //2018.06.02
/******************************************************************************
* function :  H.264@1080p@30fps+H.264@VGA@30fps			支持同时编码2路码流
******************************************************************************/
HI_S32 SAMPLE_VENC_1080P_CLASSIC(HI_VOID)  /* 最重要的函数 */
{
    PAYLOAD_TYPE_E enPayLoad[3]= {PT_H264, PT_H264,PT_H264}; /*枚举类型、数组、3路码流的编码类型 */
    PIC_SIZE_E enSize[3] = {PIC_HD1080, PIC_VGA,PIC_QVGA}; /* 3路码流的分辨率 */
	HI_U32 u32Profile = 0;
	
    VB_CONF_S stVbConf;  /* 视频缓存池 */
    SAMPLE_VI_CONFIG_S stViConfig = {0};
 
    VPSS_GRP VpssGrp;  /* 这部分是和VPSS相关的 */ 
    VPSS_CHN VpssChn;
    VPSS_GRP_ATTR_S stVpssGrpAttr;
    VPSS_CHN_ATTR_S stVpssChnAttr;
    VPSS_CHN_MODE_S stVpssChnMode;
    
    VENC_CHN VencChn;
    SAMPLE_RC_E enRcMode= SAMPLE_RC_CBR; /* 恒定比特率方式进行编码 */ /* H.264编码方式有好种,这是其中一种 */
	
    HI_S32 s32ChnNum=0;
    
    HI_S32 s32Ret = HI_SUCCESS;
    HI_U32 u32BlkSize;
    SIZE_S stSize;
    char c;
 
 
    /******************************************
     step  1: init sys variable      	## 1.这里的sys指“mpp”,指的是初始化mpp的变量 ##
    ******************************************/
    memset(&stVbConf,0,sizeof(VB_CONF_S)); /* 刷新缓存池*/
    
	SAMPLE_COMM_VI_GetSizeBySensor(&enSize[0]);  /* 获取SENSOR,在这个函数里面添加 */
    if (PIC_HD1080 == enSize[0])
    {
        enSize[1] = PIC_VGA;
		s32ChnNum = 2;
    }
    else if (PIC_HD720 == enSize[0])  /* 走的是这条路 */
    {
        enSize[1] = PIC_VGA;  	        /* 通过对VI原始图像(720P)进行裁剪、缩放成这两路(VGA、QVGA) */			
		enSize[2] = PIC_QVGA;
		s32ChnNum = 3; 		/* 这里的3表示3路码流,分别是720P、VGA、QVGA */
    }
    else
    {
        printf("not support this sensor\n");
        return HI_FAILURE;
    }
#ifdef hi3518ev201
	s32ChnNum = 1;
#endif
	printf("s32ChnNum = %d\n",s32ChnNum);
 
    stVbConf.u32MaxPoolCnt = 128;   /* 缓存池的数量为128 */
 
    /*video buffer*/
	if(s32ChnNum >= 1)  /* 走这条通道 */
    {	/* “enSize”表示传进去是多少像素的,“SAMPLE_PIXEL_FORMAT”表示像素格式(RGB888或RGB565) 
    	“SAMPLE_SYS_ALIGN_WIDTH”表示对齐*/
	    u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm,\
	                enSize[0], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
	    stVbConf.astCommPool[0].u32BlkSize = u32BlkSize;
	    stVbConf.astCommPool[0].u32BlkCnt = g_u32BlkCnt;
	}
	if(s32ChnNum >= 2)
    {
	    u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm,\
	                enSize[1], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
	    stVbConf.astCommPool[1].u32BlkSize = u32BlkSize;
	    stVbConf.astCommPool[1].u32BlkCnt =g_u32BlkCnt;
	}
	if(s32ChnNum >= 3) /*  */
    {
		u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm,\
                enSize[2], SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH);
		stVbConf.astCommPool[2].u32BlkSize = u32BlkSize;
		stVbConf.astCommPool[2].u32BlkCnt = g_u32BlkCnt;
    }
 
    /******************************************
     step 2: mpp system init.  		## 2.初始化MMP系统 ##
    ******************************************/
    s32Ret = SAMPLE_COMM_SYS_Init(&stVbConf);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("system init failed with %d!\n", s32Ret);
        goto END_VENC_1080P_CLASSIC_0;		/* 倒影式处理,MPP初始化失败直接结束编码 */
    }
 
    /******************************************
     step 3: start vi dev & chn to capture	## 3.启动输入和通道捕获 ##
    ******************************************/
    stViConfig.enViMode   = SENSOR_TYPE; 
    stViConfig.enRotate   = ROTATE_NONE;  /* 图像不旋转 */
    stViConfig.enNorm     = VIDEO_ENCODING_MODE_AUTO; /* 图像制式标准 */
    stViConfig.enViChnSet = VI_CHN_SET_NORMAL;  /*  */
    stViConfig.enWDRMode  = WDR_MODE_NONE; /* 不支持宽动态 */
    s32Ret = SAMPLE_COMM_VI_StartVi(&stViConfig);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("start vi failed!\n");
        goto END_VENC_1080P_CLASSIC_1;
    }
    
    /******************************************
     step 4: start vpss and vi bind vpss	##4:启动视频处理和绑定视频输入(通道)##
    ******************************************/
    s32Ret = SAMPLE_COMM_SYS_GetPicSize(gs_enNorm, enSize[0], &stSize);
    if (HI_SUCCESS != s32Ret)
    {
        SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
        goto END_VENC_1080P_CLASSIC_1;
    }
	if(s32ChnNum >= 1)   /* 走这个通道 */
	{
		VpssGrp = 0;
	    stVpssGrpAttr.u32MaxW = stSize.u32Width;
	    stVpssGrpAttr.u32MaxH = stSize.u32Height;
	    stVpssGrpAttr.bIeEn = HI_FALSE;
	    stVpssGrpAttr.bNrEn = HI_TRUE;
	    stVpssGrpAttr.bHistEn = HI_FALSE;
	    stVpssGrpAttr.bDciEn = HI_FALSE;
	    stVpssGrpAttr.enDieMode = VPSS_DIE_MODE_NODIE;
	    stVpssGrpAttr.enPixFmt = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
		
	    s32Ret = SAMPLE_COMM_VPSS_StartGroup(VpssGrp, &stVpssGrpAttr);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Vpss failed!\n");
	        goto END_VENC_1080P_CLASSIC_2;
	    }
 
	    s32Ret = SAMPLE_COMM_VI_BindVpss(stViConfig.enViMode);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Vi bind Vpss failed!\n");
	        goto END_VENC_1080P_CLASSIC_3;
	    }
 
		VpssChn = 0;
	    stVpssChnMode.enChnMode      = VPSS_CHN_MODE_USER;
	    stVpssChnMode.bDouble        = HI_FALSE;
	    stVpssChnMode.enPixelFormat  = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
	    stVpssChnMode.u32Width       = stSize.u32Width;
	    stVpssChnMode.u32Height      = stSize.u32Height;
	    stVpssChnMode.enCompressMode = COMPRESS_MODE_SEG;
	    memset(&stVpssChnAttr, 0, sizeof(stVpssChnAttr));
	    stVpssChnAttr.s32SrcFrameRate = -1;
	    stVpssChnAttr.s32DstFrameRate = -1;
	    s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Enable vpss chn failed!\n");
	        goto END_VENC_1080P_CLASSIC_4;
	    }
	}
 
	if(s32ChnNum >= 2)
	{
		s32Ret = SAMPLE_COMM_SYS_GetPicSize(gs_enNorm, enSize[1], &stSize);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
	        goto END_VENC_1080P_CLASSIC_4;
	    }
	    VpssChn = 1;
	    stVpssChnMode.enChnMode       = VPSS_CHN_MODE_USER;
	    stVpssChnMode.bDouble         = HI_FALSE;
	    stVpssChnMode.enPixelFormat   = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
	    stVpssChnMode.u32Width        = stSize.u32Width;
	    stVpssChnMode.u32Height       = stSize.u32Height;
	    stVpssChnMode.enCompressMode  = COMPRESS_MODE_SEG;
	    stVpssChnAttr.s32SrcFrameRate = -1;
	    stVpssChnAttr.s32DstFrameRate = -1;
	    s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Enable vpss chn failed!\n");
	        goto END_VENC_1080P_CLASSIC_4;
	    }
	}
	
 
	if(s32ChnNum >= 3)
	{	
		s32Ret = SAMPLE_COMM_SYS_GetPicSize(gs_enNorm, enSize[2], &stSize);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
	        goto END_VENC_1080P_CLASSIC_4;
	    }
		VpssChn = 2;
		stVpssChnMode.enChnMode 	= VPSS_CHN_MODE_USER;
		stVpssChnMode.bDouble		= HI_FALSE;
		stVpssChnMode.enPixelFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
		stVpssChnMode.u32Width		= stSize.u32Width;
		stVpssChnMode.u32Height 	= stSize.u32Height;
		stVpssChnMode.enCompressMode = COMPRESS_MODE_NONE;
		
		stVpssChnAttr.s32SrcFrameRate = -1;
		stVpssChnAttr.s32DstFrameRate = -1;
		
		s32Ret = SAMPLE_COMM_VPSS_EnableChn(VpssGrp, VpssChn, &stVpssChnAttr, &stVpssChnMode, HI_NULL);
		if (HI_SUCCESS != s32Ret)
		{
			SAMPLE_PRT("Enable vpss chn failed!\n");
			goto END_VENC_1080P_CLASSIC_4;
		}
	}
    /******************************************
     step 5: start stream venc  # 可以在这里叠加OSD信息,并研究哪些码流 #
    ******************************************/
    /*** HD1080P **/
    printf("\t c) cbr.\n");
    printf("\t v) vbr.\n");
    printf("\t f) fixQp\n");
    printf("please input choose rc mode!\n");
    c = (char)getchar();
    switch(c)
    {
        case 'c':
            enRcMode = SAMPLE_RC_CBR;
            break;
        case 'v':
            enRcMode = SAMPLE_RC_VBR;
            break;
        case 'f':
            enRcMode = SAMPLE_RC_FIXQP;
            break;
        default:
            printf("rc mode! is invaild!\n");
            goto END_VENC_1080P_CLASSIC_4;
    }
 
	/*** enSize[0] ***/
	if(s32ChnNum >= 1)		
	{
	    VpssGrp = 0;
	    VpssChn = 0; 	/* 媒体处理通道0 */
	    VencChn = 0;	/* 编码通道0 */
		
		/* 通道0、负载类型PT_H264、NTSC制式、PIC_HD1080()、恒定比特率编码、0 */
	    s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[0],\
	                                   gs_enNorm, enSize[0], enRcMode,u32Profile);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
 
	    s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
	}
 
	/*** enSize[1] **/
	if(s32ChnNum >= 2)		
	{
	    VpssChn = 1; /* 媒体处理通道1 */
	    VencChn = 1; /* 编码通道1 */
	    /* 通道1、负载类型PT_H264、NTSC制式、PIC_VGA(640 * 480)、恒定比特率编码、0 */
	    s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[1], \
	                                    gs_enNorm, enSize[1], enRcMode,u32Profile);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
 
	    s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
	}
	/*** enSize[2] **/
	if(s32ChnNum >= 3)			
	{
	    VpssChn = 2; /* 媒体处理通道2 */
	    VencChn = 2; /* 编码通道2 */
	    /* 通道2、负载类型PT_H264、NTSC制式、PIC_QVGA(320 * 240)、恒定比特率编码、0 */
	    s32Ret = SAMPLE_COMM_VENC_Start(VencChn, enPayLoad[2], \
	                                    gs_enNorm, enSize[2], enRcMode,u32Profile);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
 
	    s32Ret = SAMPLE_COMM_VENC_BindVpss(VencChn, VpssGrp, VpssChn);
	    if (HI_SUCCESS != s32Ret)
	    {
	        SAMPLE_PRT("Start Venc failed!\n");
	        goto END_VENC_1080P_CLASSIC_5;
	    }
	}
    /******************************************
     step 6: stream venc process -- get stream, then save it to file. 

 

你可能感兴趣的:(Machine,Learning)