This article mainly talks about the VENC code in Hisilicon Sample to introduce his experience.
/******************************************************************************
* function : main()
* Description : video venc sample
******************************************************************************/
#ifdef __HuaweiLite__
int app_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
HI_S32 s32Ret;
HI_U32 u32Index;
if (argc < 2 || argc > 2)
{
SAMPLE_VENC_Usage(argv[0]);
return HI_FAILURE;
}
if (!strncmp(argv[1], "-h", 2))
{
SAMPLE_VENC_Usage(argv[0]);
return HI_SUCCESS;
}
u32Index = atoi(argv[1]);
#ifndef __HuaweiLite__
signal(SIGINT, SAMPLE_VENC_HandleSig);
signal(SIGTERM, SAMPLE_VENC_HandleSig);
#endif
switch (u32Index)
{
case 0:
s32Ret = SAMPLE_VENC_H265_H264();
break;
case 1:
s32Ret = SAMPLE_VENC_LOW_DELAY();
break;
case 2:
s32Ret = SAMPLE_VENC_Qpmap();
break;
case 3:
s32Ret = SAMPLE_VENC_IntraRefresh();
break;
case 4:
s32Ret = SAMPLE_VENC_ROIBG();
break;
case 5:
s32Ret = SAMPLE_VENC_DeBreathEffect();
break;
case 6:
s32Ret = SAMPLE_VENC_SVC_H264();
break;
case 7:
s32Ret = SAMPLE_VENC_MJPEG_JPEG();
break;
default:
printf("the index is invaild!\n");
SAMPLE_VENC_Usage(argv[0]);
return HI_FAILURE;
}
if (HI_SUCCESS == s32Ret)
{ printf("program exit normally!\n"); }
else
{ printf("program exit abnormally!\n"); }
#ifdef __HuaweiLite__
return s32Ret;
#else
exit(s32Ret);
#endif
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
SAMPLE_VENC’s main function has seven encoding functions. Take the first SAMPLE_VENC_H265_H264 () as an example to introduce the process of Venc.
Main
SAMPLE_VENC_USAGE (/*SAMPLE usage*/)
Switch (U32Index)
Sample_venc_h265_h264 () // encoding H264, 265
Sample_comm_sys_getpicSize () //
Sample_comm_vi_getsensorinfo () //
Sample_venc_checksensor ()
Sample_venc_modifyResolution () // if hifailure run this
Sample_comm_vi_GetsizeBySor () // get entises by different sensor
Sample_comm_sys_getpicSize () // Get Piction Size (w*h), accounting enpicSize
Sample_venc_vi_init ()
Sample_venc_sys_init ()
MEMSET (& Stvbconf, 0, SIZEOF (VB_CONFIG_S)) // Clear in the cache pool memory clearing 0
SAMPLE_COMM_VI_GETSIZESOR () // Obtain image data from the Sensor type 1080p, 720p and the like
Sample_comm_sys_getpicSize () // From 1080p, 720p gets the width and high of the image
Common_getpicbuffersize () // Calculate the size of the cache block
if (0 == U32SupplementConfig)
Sample_comm_sys_init ()
Hi_mpi_sys_exit () // Go to initialize
Hi_mpi_vb_exit (); // vb to initialize
Hi_mpi_vb_setconfig () // vb set
Hi_mpi_vb_init (); // vb init
Hi_mpi_sys_init (); // MPP Init
Sample_comm_sys_initwithvbsupplement ()
Hi_mpi_sys_exit () // Go to initialize
Hi_mpi_vb_exit (); // vb to initialize
Hi_mpi_vb_setconfig ()
Hi_mpi_vb_setSupplementconfig () // Set the additional information of VB memory
Hi_mpi_vb_init ()
Hi_mpi_sys_init ();
Sample_venc_vpsS_init ()
SAMPLE_COMM_VI_GETSIZESOR () // Obtain image data from the Sensor type 1080p, 720p and the like
Sample_comm_sys_getpicSize () // From 1080p, 720p gets the width and high of the image
Sample_comm_vpsS_Start () //
Hi_mpi_vpsm_creategrp ()
Hi_mpi_vpsS_Setchnattr ()
Hi_mpi_vpsS_Startgrp ()
Sample_comm_vi_bind_vpsS () // directly call, Hisilicon Good
Hi_mpi_sys_bind ()
// Start Stream Venc
Sample_venc_getrcmode () // get cbr etc
Sample_venc_getgopmode () // get normalp etc
Sample_comm_venc_getgopattr () // Configure Gop if failure goto exit_vi_vpsS_UNBind
/*** encode h.265 **/
Sample_comm_venc_start () // if failure goto exit_vi_vpsS_unbind
Sample_comm_venc_creat ()
Sample_comm_sys_getpicSize ()
Sample_comm_vi_getsensorinfo () // Get Sensor data, configure CHN, DEV, etc.
Sample_comm_vi_getframerateBYSOR () // Get Information From Sensor and Control Framerate
Hi_mpi_venc_createchn ()
Sample_comm_venc_closreencode ()
Sample_comm_vps_bind_venc () // if failure get_venc_h265_stop
/*** encode h.264 **/
Sample_comm_venc_start () // if failure goto exit_vi_vpsS_unbind
Sample_comm_vps_bind_venc () // if failure get_venc_h264_stop
// stream save process
Sample_comm_venc_startgetstream ()
pthread_create (& gs_vencpid, 0, sample_comm_venc_getvencstreamproc, (hi_void*) & gs_stpara); // Create a thread
Sample_comm_venc_getvencstreamproc () // This Has 5 Steps and If Failure Goto EXIT_VENC_H264_UNBID
Sample_comm_venc_stopgetStream () // EXIT
Exit_venc_h264_unbind:
Sample_comm_vpsS_UNBIND_VENC (VPSSGRP, VPSSCHN [1], Vencchn [1]);
Exit_venc_h264_stop:
Sample_comm_venc_stop (vencchn [1]);
Exit_venc_h265_unbind:
Sample_comm_vpsS_UNBIND_VENC (VPSSGRP, VPSSCHN [0], Vencchn [0]);
Exit_venc_h265_stop:
Sample_comm_venc_stop (vencchn [0]);
EXIT_VI_VPSS_UNBID:
Sample_comm_vi_unbind_vpsS (vipipe, vichn, vpsgrp);
Exit_vpsS_Stop:
Sample_comm_vpsS_Stop (VPSSGRP, Abchnenable);
Exit_vi_stop:
Sample_comm_vi_stopvi (& sTViconfig);
Sample_comm_sys_exit ();
Sample_venc_low_delay () //
Sample_venc_qpmap ()
Sample_venc_intrairefresh ()
Sample_venc_roibg ()
Sample_venc_debreatheffect ()
Sample_venc_svc_h264 ()
Sample_venc_mjpeg_jpeg ()
Sample_venc_usage ()
The picture above
is the diagram of my graphics of SAMPLE_VENC_H265_H264 (), and the represented sub -function is called by the previous level, and a small number of comments are attached.
/******************************************************************************
* function: H.265e + [email protected], H.265 Channel resolution adaptable with sensor
******************************************************************************/
HI_S32 SAMPLE_VENC_H265_H264(void)
{
HI_S32 i;
HI_S32 s32Ret;
SIZE_S stSize[2];
PIC_SIZE_E enSize[2] = {PIC_3840x2160, PIC_720P};
HI_S32 s32ChnNum = 2;
VENC_CHN VencChn[2] = {0,1};
HI_U32 u32Profile[2] = {0,0};
PAYLOAD_TYPE_E enPayLoad[2] = {PT_H265, PT_H264};
VENC_GOP_MODE_E enGopMode;
VENC_GOP_ATTR_S stGopAttr;
SAMPLE_RC_E enRcMode;
HI_BOOL bRcnRefShareBuf = HI_TRUE;
VI_DEV ViDev = 0;
VI_PIPE ViPipe = 0;
VI_CHN ViChn = 0;
SAMPLE_VI_CONFIG_S stViConfig;
VPSS_GRP VpssGrp = 0;
VPSS_CHN VpssChn[2] = {0,1};
HI_BOOL abChnEnable[VPSS_MAX_PHY_CHN_NUM] = {1,1,0};
HI_U32 u32SupplementConfig = HI_FALSE;
for(i=0; i<s32ChnNum; i++)
{
s32Ret = SAMPLE_COMM_SYS_GetPicSize(enSize[i], &stSize[i]);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n");
return s32Ret;
}
}
SAMPLE_COMM_VI_GetSensorInfo(&stViConfig);
if(SAMPLE_SNS_TYPE_BUTT == stViConfig.astViInfo[0].stSnsInfo.enSnsType)
{
SAMPLE_PRT("Not set SENSOR%d_TYPE !\n",0);
return HI_FAILURE;
}
s32Ret = SAMPLE_VENC_CheckSensor(stViConfig.astViInfo[0].stSnsInfo.enSnsType,stSize[0]);
if(s32Ret != HI_SUCCESS)
{
s32Ret = SAMPLE_VENC_ModifyResolution(stViConfig.astViInfo[0].stSnsInfo.enSnsType,&enSize[0],&stSize[0]);
if(s32Ret != HI_SUCCESS)
{
return HI_FAILURE;
}
}
stViConfig.s32WorkingViNum = 1;
stViConfig.astViInfo[0].stDevInfo.ViDev = ViDev;
stViConfig.astViInfo[0].stPipeInfo.aPipe[0] = ViPipe;
stViConfig.astViInfo[0].stChnInfo.ViChn = ViChn;
stViConfig.astViInfo[0].stChnInfo.enDynamicRange = DYNAMIC_RANGE_SDR8;
stViConfig.astViInfo[0].stChnInfo.enPixFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420;
s32Ret = SAMPLE_VENC_VI_Init(&stViConfig, HI_FALSE,u32SupplementConfig);
if(s32Ret != HI_SUCCESS)
{
SAMPLE_PRT("Init VI err for %#x!\n", s32Ret);
return HI_FAILURE;
}
s32Ret = SAMPLE_VENC_VPSS_Init(VpssGrp,abChnEnable,DYNAMIC_RANGE_SDR8,PIXEL_FORMAT_YVU_SEMIPLANAR_420,stSize,stViConfig.astViInfo[0].stSnsInfo.enSnsType);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Init VPSS err for %#x!\n", s32Ret);
goto EXIT_VI_STOP;
}
s32Ret = SAMPLE_COMM_VI_Bind_VPSS(ViPipe, ViChn, VpssGrp);
if(s32Ret != HI_SUCCESS)
{
SAMPLE_PRT("VI Bind VPSS err for %#x!\n", s32Ret);
goto EXIT_VPSS_STOP;
}
/******************************************
start stream venc
******************************************/
enRcMode = SAMPLE_VENC_GetRcMode();
enGopMode = SAMPLE_VENC_GetGopMode();
s32Ret = SAMPLE_COMM_VENC_GetGopAttr(enGopMode,&stGopAttr);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Venc Get GopAttr for %#x!\n", s32Ret);
goto EXIT_VI_VPSS_UNBIND;
}
/***encode h.265 **/
s32Ret = SAMPLE_COMM_VENC_Start(VencChn[0], enPayLoad[0],enSize[0], enRcMode,u32Profile[0],bRcnRefShareBuf,&stGopAttr);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Venc Start failed for %#x!\n", s32Ret);
goto EXIT_VI_VPSS_UNBIND;
}
s32Ret = SAMPLE_COMM_VPSS_Bind_VENC(VpssGrp, VpssChn[0],VencChn[0]);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Venc Get GopAttr failed for %#x!\n", s32Ret);
goto EXIT_VENC_H265_STOP;
}
/***encode h.264 **/
s32Ret = SAMPLE_COMM_VENC_Start(VencChn[1], enPayLoad[1], enSize[1], enRcMode,u32Profile[1],bRcnRefShareBuf,&stGopAttr);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Venc Start failed for %#x!\n", s32Ret);
goto EXIT_VENC_H265_UnBind;
}
s32Ret = SAMPLE_COMM_VPSS_Bind_VENC(VpssGrp, VpssChn[1],VencChn[1]);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Venc bind Vpss failed for %#x!\n", s32Ret);
goto EXIT_VENC_H264_STOP;
}
/******************************************
stream save process
******************************************/
s32Ret = SAMPLE_COMM_VENC_StartGetStream(VencChn,s32ChnNum);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_PRT("Start Venc failed!\n");
goto EXIT_VENC_H264_UnBind;
}
printf("please press twice ENTER to exit this sample\n");
getchar();
getchar();
/******************************************
exit process
******************************************/
SAMPLE_COMM_VENC_StopGetStream();
EXIT_VENC_H264_UnBind:
SAMPLE_COMM_VPSS_UnBind_VENC(VpssGrp,VpssChn[1],VencChn[1]);
EXIT_VENC_H264_STOP:
SAMPLE_COMM_VENC_Stop(VencChn[1]);
EXIT_VENC_H265_UnBind:
SAMPLE_COMM_VPSS_UnBind_VENC(VpssGrp,VpssChn[0],VencChn[0]);
EXIT_VENC_H265_STOP:
SAMPLE_COMM_VENC_Stop(VencChn[0]);
EXIT_VI_VPSS_UNBIND:
SAMPLE_COMM_VI_UnBind_VPSS(ViPipe,ViChn,VpssGrp);
EXIT_VPSS_STOP:
SAMPLE_COMM_VPSS_Stop(VpssGrp,abChnEnable);
EXIT_VI_STOP:
SAMPLE_COMM_VI_StopVi(&stViConfig);
SAMPLE_COMM_SYS_Exit();
return s32Ret;
}
I wanted to write a detailed explanation, but too much, I don’t want to do it anymore.
is a bit strange. If I inserted the SD card, SAMPLE_VENC failed to run, and reBoot was not working. I need to re -call. I used mount and umount commands when I inserted. Essence
Next to see how the audio is tidy, get a program to record the audio and video for a period of time, and share it with everyone.