From a9ec37df9b3e6381dcb869a8314d4af2fdd639f4 Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Mon, 17 Feb 2025 16:49:58 +0800 Subject: [PATCH] Rewrite isp module --- src/main.cpp | 85 ++++--- src/modules/isp/eb3516_video.c | 422 --------------------------------- src/modules/isp/eb3516_video.h | 37 --- src/modules/isp/isp.c | 382 +++++++++++++++++++++++++++++ src/modules/isp/isp.h | 79 ++++++ src/modules/isp/snap.c | 249 +++++++++++++++++++ src/modules/isp/snap.h | 26 ++ src/modules/isp/video.c | 157 ++++++++++++ src/modules/isp/video.h | 20 ++ src/modules/util.h | 14 +- 10 files changed, 967 insertions(+), 504 deletions(-) delete mode 100755 src/modules/isp/eb3516_video.c delete mode 100755 src/modules/isp/eb3516_video.h create mode 100644 src/modules/isp/isp.c create mode 100644 src/modules/isp/isp.h create mode 100644 src/modules/isp/snap.c create mode 100644 src/modules/isp/snap.h create mode 100644 src/modules/isp/video.c create mode 100644 src/modules/isp/video.h diff --git a/src/main.cpp b/src/main.cpp index c234f56..748cab2 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,56 +1,65 @@ -#include #include +#include -#include "hi_common.h" -#include "sample_comm.h" #include "zlog.h" -#include "isp/eb3516_video.h" +#include "isp/isp.h" +#include "isp/snap.h" +#include "isp/video.h" #include "nnie/eb3516_nnie.h" -int main(int argc, char** argv) { - int result = 0; - std::error_code file_error; +int main(int argc, char **argv) { + int result = 0; + std::error_code file_error; - zlog_category_t *log = nullptr; + zlog_category_t *log = nullptr; - // Init log and check log.conf - result = zlog_init("log.conf"); - if (result) { - std::cout << "zlog init failed! Please check log.conf!\n"; - return (-1); - } + // Init log and check log.conf + result = zlog_init("log.conf"); + if (result) { + std::cout << "zlog init failed! Please check log.conf!\n"; + return (-1); + } - // Get log cat for main to print - log = zlog_get_category("cat"); - if (!log) { - std::cout << "Main get log failed!\n"; - return (-2); - } - zlog_info(log, "Init zlog Sucessfully"); + // Get log cat for main to print + log = zlog_get_category("cat"); + if (!log) { + std::cout << "Main get log failed!\n"; + return (-2); + } + zlog_info(log, "Init zlog Sucessfully"); - if (!std::experimental::filesystem::remove_all("log", file_error)) { - zlog_warn(log, "Can't Remove Last Logs FOR %s", file_error.message().c_str()); - } + if (!std::experimental::filesystem::remove_all("log", file_error)) { + zlog_warn(log, "Can't Remove Last Logs FOR %s", + file_error.message().c_str()); + } - if (!std::experimental::filesystem::create_directory("log", file_error)) { - zlog_warn(log, "Can't Create log dir FOR %s", file_error.message().c_str()); - } + if (!std::experimental::filesystem::create_directory("log", file_error)) { + zlog_warn(log, "Can't Create log dir FOR %s", file_error.message().c_str()); + } - eb3516VideoInit(); - eb3516VideoStart(); - // eb3516Yolov3Init("./coco_yolov3_detect.wk", "./dog_bike_car_416x416.bgr"); - // nnie_init(); - // nnie_start(); + // eb3516VideoInit(); + // eb3516VideoStart(); + // eb3516Yolov3Init("./coco_yolov3_detect.wk", "./dog_bike_car_416x416.bgr"); + // nnie_init(); + // nnie_start(); - PAUSE(); + ispInit(0, 1); + // ispStartVideo(); + ispStartSnap(); - // nnie_stop(); - // eb3516Yolov3Exit(); - eb3516VideoStop(); + ispSnapByPressKey(); + PAUSE(); - zlog_fini(); + ispStopSnap(); + // ispStopVideo(); - return 0; + // nnie_stop(); + // eb3516Yolov3Exit(); + // eb3516VideoStop(); + + zlog_fini(); + + return 0; } diff --git a/src/modules/isp/eb3516_video.c b/src/modules/isp/eb3516_video.c deleted file mode 100755 index ecb6548..0000000 --- a/src/modules/isp/eb3516_video.c +++ /dev/null @@ -1,422 +0,0 @@ -#include "eb3516_video.h" - -#include "hi_comm_snap.h" -#include "hi_common.h" -#include "hi_debug.h" -#include "hi_type.h" -#include "mpi_snap.h" -#include "sample_comm.h" -#include "zlog.h" - -#define VB_YUV_COUNT 10 -#define VB_RGB_COUNT 4 -#define ALIGN_DOWN_SIZE 2 - -typedef struct hiSAMPLE_VPSS_CONFIG_S { - VPSS_GRP s32GrpId; - VPSS_GRP_ATTR_S stGrpAttr; - HI_BOOL abChnEnable[VPSS_MAX_PHY_CHN_NUM]; - VPSS_CHN_ATTR_S astChnAttrs[VPSS_MAX_PHY_CHN_NUM]; -} SAMPLE_VPSS_CONFIG_S; - -static zlog_category_t *log_video = NULL; - -// Sensor Info -static SAMPLE_VI_CONFIG_S g_stViConfig = {0}; -static PIC_SIZE_E enPicSize; -static SIZE_S stSize; -static HI_U32 u32Framerate; - -static VB_CONFIG_S g_stVbConfig = {0}; -static SAMPLE_VO_CONFIG_S stVoConfig; -static SAMPLE_VPSS_CONFIG_S g_stVpss0Config = {0}; -static SAMPLE_VPSS_CONFIG_S g_stVpss1Config = {0}; -static VI_PIPE videoPipe = 0; -static VI_PIPE snapPipe = 1; - -// 注意此处不能为通道 0, 因为 hi3516 的 VPSS 通道 0 只支持放大, -// 其它通道只支持缩小 -static const VPSS_CHN g_Vpss0Chn[2] = {1, 2}; -static const VPSS_CHN g_Vpss1Chn = 1; -static const PIC_SIZE_E g_PicSizes[3] = {PIC_1080P, PIC_720P, PIC_1080P}; -static const VENC_CHN g_VencChn[3] = {0, 1, 2}; -static const PAYLOAD_TYPE_E g_CodecTypes[3] = {PT_H265, PT_H264, PT_JPEG}; - -static HI_S32 VPSS_GetDefChnAttr(VPSS_CHN_ATTR_S *pstVpssChnAttr, - VPSS_CHN VpssChn, PIC_SIZE_E enPicSize) { - SIZE_S stSize; - SAMPLE_COMM_SYS_GetPicSize(enPicSize, &stSize); - pstVpssChnAttr->enChnMode = VPSS_CHN_MODE_USER; - pstVpssChnAttr->u32Width = stSize.u32Width; - pstVpssChnAttr->u32Height = stSize.u32Height; - pstVpssChnAttr->enVideoFormat = VIDEO_FORMAT_LINEAR; - pstVpssChnAttr->enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420; - pstVpssChnAttr->enDynamicRange = DYNAMIC_RANGE_SDR8; - pstVpssChnAttr->enCompressMode = COMPRESS_MODE_NONE; // COMPRESS_MODE_SEG; - pstVpssChnAttr->stFrameRate.s32SrcFrameRate = -1; - pstVpssChnAttr->stFrameRate.s32DstFrameRate = -1; - pstVpssChnAttr->u32Depth = 0; - pstVpssChnAttr->bMirror = HI_FALSE; - pstVpssChnAttr->bFlip = HI_FALSE; - pstVpssChnAttr->stAspectRatio.enMode = ASPECT_RATIO_NONE; - return HI_SUCCESS; -} - -static HI_S32 VPSS_GetParams(SAMPLE_VPSS_CONFIG_S *pstVpssConfig, - SIZE_S *pstMaxSize, HI_BOOL bSnap) { - // 设置 VPSS 组 - pstVpssConfig->s32GrpId = !bSnap ? 0 : 1; - pstVpssConfig->stGrpAttr.u32MaxW = pstMaxSize->u32Width; - pstVpssConfig->stGrpAttr.u32MaxH = pstMaxSize->u32Width; - pstVpssConfig->stGrpAttr.enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420; - pstVpssConfig->stGrpAttr.enDynamicRange = DYNAMIC_RANGE_SDR8; - pstVpssConfig->stGrpAttr.stFrameRate.s32SrcFrameRate = -1; - pstVpssConfig->stGrpAttr.stFrameRate.s32DstFrameRate = -1; - pstVpssConfig->stGrpAttr.bNrEn = HI_TRUE; - pstVpssConfig->stGrpAttr.stNrAttr.enNrType = - !bSnap ? VPSS_NR_TYPE_VIDEO : VPSS_NR_TYPE_SNAP; - pstVpssConfig->stGrpAttr.stNrAttr.enCompressMode = COMPRESS_MODE_FRAME; - pstVpssConfig->stGrpAttr.stNrAttr.enNrMotionMode = NR_MOTION_MODE_NORMAL; - // 设置 VPSS 通道 - if (!bSnap) { - for (HI_S32 i = 0; i < ARRAY_LENGTH(g_Vpss0Chn); i++) { - VPSS_CHN VpssChn = g_Vpss0Chn[i]; - pstVpssConfig->abChnEnable[VpssChn] = HI_TRUE; - VPSS_GetDefChnAttr(&pstVpssConfig->astChnAttrs[VpssChn], VpssChn, - g_PicSizes[i]); - if (i == 1) { - pstVpssConfig->astChnAttrs[VpssChn].u32Depth = 2; - } - } - } else { - VPSS_CHN VpssChn = g_Vpss1Chn; - pstVpssConfig->abChnEnable[VpssChn] = HI_TRUE; - VPSS_GetDefChnAttr(&pstVpssConfig->astChnAttrs[VpssChn], VpssChn, - g_PicSizes[2]); - } - return HI_SUCCESS; -} - -HI_BOOL eb3516VideoInit(void) { - log_video = zlog_get_category("eb3516_video"); - if (!log_video) { - return HI_FALSE; - } - - // Get Sensor(VI) Info: picture size , frame rate - SAMPLE_COMM_VI_GetSensorInfo(&g_stViConfig); - SAMPLE_COMM_VI_GetSizeBySensor(g_stViConfig.astViInfo[0].stSnsInfo.enSnsType, - &enPicSize); - SAMPLE_COMM_SYS_GetPicSize(enPicSize, &stSize); - SAMPLE_COMM_VI_GetFrameRateBySensor( - g_stViConfig.astViInfo[0].stSnsInfo.enSnsType, &u32Framerate); - - // 配置图像缓冲区 - g_stVbConfig.u32MaxPoolCnt = 2; - // 获取一帧图片 buffer 的大小 - g_stVbConfig.astCommPool[0].u64BlkSize = COMMON_GetPicBufferSize( - stSize.u32Width, stSize.u32Height, PIXEL_FORMAT_YVU_SEMIPLANAR_422, - DATA_BITWIDTH_8, COMPRESS_MODE_SEG, DEFAULT_ALIGN); - g_stVbConfig.astCommPool[0].u32BlkCnt = VB_YUV_COUNT; - // 获取 raw buffer 的大小 - g_stVbConfig.astCommPool[1].u64BlkSize = VI_GetRawBufferSize( - stSize.u32Width, stSize.u32Height, PIXEL_FORMAT_RGB_BAYER_16BPP, - COMPRESS_MODE_NONE, DEFAULT_ALIGN); - g_stVbConfig.astCommPool[1].u32BlkCnt = VB_RGB_COUNT; - - // 配置 VI - g_stViConfig.s32WorkingViNum = 1; - g_stViConfig.as32WorkingViId[0] = 0; - // 设置 VI 传感器信息 - g_stViConfig.astViInfo[0].stSnsInfo.MipiDev = - SAMPLE_COMM_VI_GetComboDevBySensor( - g_stViConfig.astViInfo[0].stSnsInfo.enSnsType, 0); - - g_stViConfig.astViInfo[0].stDevInfo.ViDev = 0; - g_stViConfig.astViInfo[0].stDevInfo.enWDRMode = WDR_MODE_NONE; - // 设置 VI PIPE 信息 - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[0] = videoPipe; // video pipe - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1] = snapPipe; // snap pipe - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[2] = -1; - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[3] = -1; - g_stViConfig.astViInfo[0].stPipeInfo.enMastPipeMode = VI_OFFLINE_VPSS_OFFLINE; - // 设置 VI 通道信息 - g_stViConfig.astViInfo[0].stChnInfo.ViChn = 0; - g_stViConfig.astViInfo[0].stChnInfo.enPixFormat = - PIXEL_FORMAT_YVU_SEMIPLANAR_420; - g_stViConfig.astViInfo[0].stChnInfo.enDynamicRange = DYNAMIC_RANGE_SDR8; - g_stViConfig.astViInfo[0].stChnInfo.enVideoFormat = VIDEO_FORMAT_LINEAR; - g_stViConfig.astViInfo[0].stChnInfo.enCompressMode = COMPRESS_MODE_SEG; - // 设置 VI 抓拍信息 - g_stViConfig.astViInfo[0].stSnapInfo.bSnap = HI_TRUE; - g_stViConfig.astViInfo[0].stSnapInfo.bDoublePipe = HI_TRUE; - g_stViConfig.astViInfo[0].stSnapInfo.VideoPipe = videoPipe; - g_stViConfig.astViInfo[0].stSnapInfo.SnapPipe = snapPipe; - g_stViConfig.astViInfo[0].stSnapInfo.enVideoPipeMode = - VI_OFFLINE_VPSS_OFFLINE; - g_stViConfig.astViInfo[0].stSnapInfo.enSnapPipeMode = VI_OFFLINE_VPSS_OFFLINE; - - // 配置 VPSS - VPSS_GetParams(&g_stVpss0Config, &stSize, HI_FALSE); - VPSS_GetParams(&g_stVpss1Config, &stSize, HI_TRUE); - - zlog_debug(log_video, "eb3516 Video Init Finish..."); - return HI_TRUE; -} - -HI_S32 eb3516VideoStart(void) { - HI_S32 s32Ret = 0; - - // 初始化 VB 和 MPI 系统 - s32Ret = SAMPLE_COMM_SYS_InitWithVbSupplement(&g_stVbConfig, - VB_SUPPLEMENT_JPEG_MASK); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Init VB and MPI failed with %#x!\n", s32Ret); - goto EXIT_SYS_RST_CLK; - } - - // 启动 VI - s32Ret = SAMPLE_COMM_VI_SetParam(&g_stViConfig); - ISP_CTRL_PARAM_S stIspCtrlParam = {0}; - memset(&stIspCtrlParam, 0, sizeof(stIspCtrlParam)); - s32Ret = HI_MPI_ISP_GetCtrlParam( - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[0], &stIspCtrlParam); - stIspCtrlParam.u32StatIntvl = u32Framerate / 30; - s32Ret = HI_MPI_ISP_SetCtrlParam( - g_stViConfig.astViInfo[0].stPipeInfo.aPipe[0], &stIspCtrlParam); - s32Ret = SAMPLE_COMM_VI_StartVi(&g_stViConfig); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Start VI failed with %#x!\n", s32Ret); - goto EXIT_SYS_STOP; - } - zlog_debug(log_video, "eb3516 VI Start Success..."); - - // 启动 VPSS - s32Ret = SAMPLE_COMM_VPSS_Start( - g_stVpss0Config.s32GrpId, g_stVpss0Config.abChnEnable, - &g_stVpss0Config.stGrpAttr, g_stVpss0Config.astChnAttrs); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Start VPSS0 group %d failed with %#x!\n", - g_stVpss0Config.s32GrpId, s32Ret); - goto EXIT_VI_STOP; - } - zlog_debug(log_video, "eb3516 VPSS0 Start Success..."); - - s32Ret = SAMPLE_COMM_VPSS_Start( - g_stVpss1Config.s32GrpId, g_stVpss1Config.abChnEnable, - &g_stVpss1Config.stGrpAttr, g_stVpss1Config.astChnAttrs); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Start VPSS1 group %d failed with %#x!\n", - g_stVpss1Config.s32GrpId, s32Ret); - goto EXIT_VI_STOP; - } - zlog_debug(log_video, "eb3516 VPSS1 Start Success..."); - - // 绑定 VI->VPSS - s32Ret = SAMPLE_COMM_VI_Bind_VPSS(videoPipe, - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss0Config.s32GrpId); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "VI Video Pipe bind VPSS0 failed with %#x!\n", - s32Ret); - goto EXIT_VPSS_STOP; - } - zlog_debug(log_video, "eb3516 VI Bind VPSS0 Success"); - s32Ret = SAMPLE_COMM_VI_Bind_VPSS(snapPipe, - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss1Config.s32GrpId); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "VI Snap Pipe bind VPSS1 failed with %#x!\n", s32Ret); - goto EXIT_VPSS_STOP; - } - zlog_debug(log_video, "eb3516 VI Bind VPSS1 Success"); - - // Get Venc Config - VENC_GOP_ATTR_S stGopAttr; - SAMPLE_COMM_VENC_GetGopAttr(VENC_GOPMODE_NORMALP, &stGopAttr); - - // 编码 h.265 4k - s32Ret = SAMPLE_COMM_VENC_Creat(g_VencChn[0], g_CodecTypes[0], g_PicSizes[0], - SAMPLE_RC_VBR, 0, HI_FALSE, &stGopAttr); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Create Venc Chn %d failed with %#x!\n", g_VencChn[0], - s32Ret); - goto EXIT_VI_VPSS_UNBIND; - } - zlog_debug(log_video, "eb3516 VENC Start Success..."); - - // 绑定 VPSS[0,1]->VENC[0] - s32Ret = SAMPLE_COMM_VPSS_Bind_VENC(g_stVpss0Config.s32GrpId, g_Vpss0Chn[0], - g_VencChn[0]); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Venc bind Vpss failed with %#x!\n", s32Ret); - goto VENC_H265_STOP; - } - - /*config vo*/ - SAMPLE_COMM_VO_GetDefConfig(&stVoConfig); - stVoConfig.enDstDynamicRange = DYNAMIC_RANGE_SDR8; - stVoConfig.enVoIntfType = VO_INTF_HDMI; - stVoConfig.enPicSize = enPicSize; - - /*start vo*/ - s32Ret = SAMPLE_COMM_VO_StartVO(&stVoConfig); - if (HI_SUCCESS != s32Ret) { - SAMPLE_PRT("start vo failed. s32Ret: 0x%x !\n", s32Ret); - goto EXIT_VENC_H264_STOP; - } - zlog_debug(log_video, "eb3516 VO Start Success..."); - - /*vpss bind vo for hdmi output*/ - // Bind VPSS[0,1] -> VENC[0] -> VO(HDMI) - s32Ret = SAMPLE_COMM_VPSS_Bind_VO(g_stVpss0Config.s32GrpId, g_Vpss0Chn[0], - stVoConfig.VoDev, g_VencChn[0]); - if (HI_SUCCESS != s32Ret) { - SAMPLE_PRT("vo bind vpss failed. s32Ret: 0x%x !\n", s32Ret); - goto EXIT_VO_STOP; - } - - // 抓拍 jpeg 4k - SIZE_S stPicSize; - SAMPLE_COMM_SYS_GetPicSize(g_PicSizes[2], &stPicSize); - s32Ret = SAMPLE_COMM_VENC_SnapStart(g_VencChn[2], &stPicSize, HI_TRUE); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Venc Snap Start failed with %#x\n", s32Ret); - goto EXIT_VENC_H264_UNBIND; - } - // 绑定 VPSS[1,1]->VENC[2] for snap pic - s32Ret = SAMPLE_COMM_VPSS_Bind_VENC(g_stVpss1Config.s32GrpId, g_Vpss1Chn, - g_VencChn[2]); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "Venc bind Vpss failed with %#x!\n", s32Ret); - goto EXIT_VENC_SNAP_STOP; - } - - // 启动抓拍通道 - SNAP_ATTR_S stSnapAttr; - stSnapAttr.enSnapType = SNAP_TYPE_NORMAL; - stSnapAttr.bLoadCCM = HI_TRUE; - stSnapAttr.stNormalAttr.u32FrameCnt = 1; - stSnapAttr.stNormalAttr.u32RepeatSendTimes = 1; - stSnapAttr.stNormalAttr.bZSL = HI_FALSE; - s32Ret = HI_MPI_SNAP_SetPipeAttr(snapPipe, &stSnapAttr); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "HI_MPI_SNAP_SetPipeAttr failed with %#x!\n", s32Ret); - goto EXIT_VO_STOP; - } - s32Ret = HI_MPI_SNAP_EnablePipe(snapPipe); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "HI_MPI_SNAP_EnablePipe failed with %#x!\n", s32Ret); - goto EXIT_VO_STOP; - } - - // 设置照片 EXIF 信息 - ISP_DCF_INFO_S stIspDCF; - HI_MPI_ISP_GetDCFInfo(snapPipe, &stIspDCF); - strncpy((char *)stIspDCF.stIspDCFConstInfo.au8ImageDescription, "", - DCF_DRSCRIPTION_LENGTH); - strncpy((char *)stIspDCF.stIspDCFConstInfo.au8Make, "OurEDA", - DCF_DRSCRIPTION_LENGTH); - strncpy((char *)stIspDCF.stIspDCFConstInfo.au8Model, "CatFeeder Camera", - DCF_DRSCRIPTION_LENGTH); - snprintf((char *)stIspDCF.stIspDCFConstInfo.au8Software, - DCF_DRSCRIPTION_LENGTH, "CatFeeder snap"); - s32Ret = HI_MPI_ISP_SetDCFInfo(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1], - &stIspDCF); - if (s32Ret != HI_SUCCESS) { - zlog_error(log_video, "HI_MPI_ISP_SetDCFInfo failed with %#x!\n", s32Ret); - } - - // // Start Venc Stream - // s32Ret = SAMPLE_COMM_VENC_StartGetStream( - // g_VencChn + 1, sizeof(g_VencChn) / sizeof(VENC_CHN) - 2); - // if (HI_SUCCESS != s32Ret) { - // zlog_error(log_video, "Get venc stream failed!\n"); - // goto EXIT_VENC_H264_STOP; - // } - // zlog_debug(log_video, "eb3516 Stream Start Success..."); - - printf("=======press any key to trigger=====\n"); - getchar(); - - s32Ret = - HI_MPI_SNAP_TriggerPipe(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1]); - if (HI_SUCCESS != s32Ret) { - SAMPLE_PRT("HI_MPI_SNAP_TriggerPipe failed with %#x!\n", s32Ret); - } - - s32Ret = SAMPLE_COMM_VENC_SaveJpeg(g_VencChn[2], 1); - if (HI_SUCCESS != s32Ret) { - zlog_error(log_video, "Save Jpeg Failed"); - } - - zlog_info(log_video, "Mpp Start Successfully"); - // PAUSE(); - - SAMPLE_COMM_VENC_StopGetStream(); - return true; - -EXIT_VENC_SNAP_STOP: - SAMPLE_COMM_VENC_Stop(g_VencChn[2]); -EXIT_VO_STOP: - SAMPLE_COMM_VO_StopVO(&stVoConfig); -EXIT_VENC_H264_STOP: - SAMPLE_COMM_VENC_Stop(g_VencChn[1]); -EXIT_VENC_H264_UNBIND: - // SAMPLE_COMM_VPSS_UnBind_VENC(g_stVpss0Config.s32GrpId, g_Vpss0Chn[1], - // g_VencChn[1]); -VENC_H265_STOP: - SAMPLE_COMM_VENC_Stop(g_VencChn[0]); -EXIT_VI_VPSS_UNBIND: - SAMPLE_COMM_VI_UnBind_VPSS(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[0], - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss0Config.s32GrpId); - SAMPLE_COMM_VI_UnBind_VPSS(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1], - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss1Config.s32GrpId); -EXIT_VPSS_STOP: - SAMPLE_COMM_VPSS_Stop(g_stVpss0Config.s32GrpId, g_stVpss0Config.abChnEnable); - SAMPLE_COMM_VPSS_Stop(g_stVpss1Config.s32GrpId, g_stVpss1Config.abChnEnable); -EXIT_VI_STOP: - SAMPLE_COMM_VI_StopVi(&g_stViConfig); -EXIT_SYS_STOP: - SAMPLE_COMM_SYS_Exit(); -EXIT_SYS_RST_CLK: -EXIT: - return s32Ret; -} - -void eb3516VideoStop(void) { - SAMPLE_COMM_VENC_Stop(g_VencChn[2]); - SAMPLE_COMM_VO_StopVO(&stVoConfig); - SAMPLE_COMM_VENC_Stop(g_VencChn[1]); - SAMPLE_COMM_VENC_Stop(g_VencChn[0]); - SAMPLE_COMM_VI_UnBind_VPSS(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[0], - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss0Config.s32GrpId); - SAMPLE_COMM_VI_UnBind_VPSS(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1], - g_stViConfig.astViInfo[0].stChnInfo.ViChn, - g_stVpss1Config.s32GrpId); - SAMPLE_COMM_VPSS_Stop(g_stVpss0Config.s32GrpId, g_stVpss0Config.abChnEnable); - SAMPLE_COMM_VPSS_Stop(g_stVpss1Config.s32GrpId, g_stVpss1Config.abChnEnable); - SAMPLE_COMM_VI_StopVi(&g_stViConfig); - SAMPLE_COMM_SYS_Exit(); - - zlog_info(log_video, "Finish Stopping eb3516 Video"); -} - -HI_S32 eb3516GetPic(void) { - HI_S32 ret = HI_SUCCESS; - - VIDEO_FRAME_INFO_S stRawInfo; - HI_S32 milliSec = 10000; - - ret = HI_MPI_VI_GetPipeFrame(g_stViConfig.astViInfo[0].stPipeInfo.aPipe[1], - &stRawInfo, milliSec); - if (HI_SUCCESS != ret) { - zlog_error(log_video, "Get Pipe Frame Failed"); - return ret; - } - - return ret; -} \ No newline at end of file diff --git a/src/modules/isp/eb3516_video.h b/src/modules/isp/eb3516_video.h deleted file mode 100755 index 37cb757..0000000 --- a/src/modules/isp/eb3516_video.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef __EB3516_VIDEO_H__ -#define __EB3516_VIDEO_H__ - -#include "hi_type.h" -#ifdef __cplusplus -extern "C"{ -#endif - -#include "common.h" - -typedef enum { - VIDEO = 0, - H264, - H265, - JPEG, - - AUDIO = 128, - AAC, - G711A, // PCMA - G711U, // PCMU -} codec_type_t; - -typedef struct { - uint8_t *data; - uint32_t length; - uint64_t timestamp; // in microseconds -} video_packet_t; - -HI_BOOL eb3516VideoInit(void); -HI_S32 eb3516VideoStart(void); -void eb3516VideoStop(void); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/src/modules/isp/isp.c b/src/modules/isp/isp.c new file mode 100644 index 0000000..3848023 --- /dev/null +++ b/src/modules/isp/isp.c @@ -0,0 +1,382 @@ +#include "isp.h" + +#include "common.h" +#include "hi_comm_vb.h" +#include "hi_comm_video.h" +#include "hi_common.h" +#include "hi_defines.h" +#include "hi_type.h" +#include "sample_comm.h" + +#include +#include + +#define VB_YUV_COUNT 10 +#define VB_RGB_COUNT 4 +#define ALIGN_DOWN_SIZE 2 + +zlog_category_t *log_isp = NULL; + +// ******************** +// * ISP Configuration* +// ******************** +const PIXEL_FORMAT_E ISP_PIXEL_FORMAT = PIXEL_FORMAT_YVU_SEMIPLANAR_422; + +const COMPRESS_MODE_E ISP_COMPRESS_MODE = COMPRESS_MODE_SEG; + +// VI Configuration +SAMPLE_VI_CONFIG_S vi_config; +PIC_SIZE_E vi_pic_size_type = 0; + +// VPSS Configuration +// Video VPSS +isp_vpss_config_t vpss_video_config = {0}; +VPSS_CHN _vpss_video_chn[2] = {1, 2}; +isp_vpss_chn_t vpss_video_chn = {_vpss_video_chn, + ARRAY_LENGTH(_vpss_video_chn)}; +// Snap VPSS +isp_vpss_config_t vpss_snap_config = {0}; +VPSS_CHN _vpss_snap_chn[1] = {1}; +isp_vpss_chn_t vpss_snap_chn = {_vpss_snap_chn, ARRAY_LENGTH(_vpss_snap_chn)}; + +// VENC Configurationk +VENC_CHN venc_video_chn = 0; +VENC_CHN venc_snap_chn = 1; + +// VO Configuration +SAMPLE_VO_CONFIG_S vo_config = {0}; + +static HI_BOOL is_isp_working = HI_FALSE; + +/** + * @brief + * + * @param video_pipe + * @param snap_pipe + * @return HI_S32 + */ +HI_S32 ispInit(VI_PIPE video_pipe, VI_PIPE snap_pipe) { + HI_S32 ret = HI_SUCCESS; + // VI Info + SIZE_S pic_size = {0}; + HI_U32 frame_rate = 0; + + // VB Info + VB_CONFIG_S vb_config = {0}; + + // Init zlog + log_isp = zlog_get_category("eb3516_isp"); + if (!log_isp) { + printf("Failure: Failed to init zlog\n"); + return HI_FAILURE; + } + + // *************************** + // * Step1: VI Configuration * + // *************************** + // Get Sensor(VI) Info + memset(&vi_config, 0, sizeof(SAMPLE_VI_CONFIG_S)); + SAMPLE_COMM_VI_GetSensorInfo(&vi_config); + + // Get frame rate + ret = SAMPLE_COMM_VI_GetFrameRateBySensor( + vi_config.astViInfo[0].stSnsInfo.enSnsType, &frame_rate); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Get sensor frame rate failed with %d", ret); + goto EXIT1; + } + + // Input Device Info Configuration + // Input Device Number + vi_config.s32WorkingViNum = 1; + vi_config.as32WorkingViId[0] = 0; + // Sensor Info + vi_config.astViInfo[0].stSnsInfo.MipiDev = SAMPLE_COMM_VI_GetComboDevBySensor( + vi_config.astViInfo[0].stSnsInfo.enSnsType, 0); + + vi_config.astViInfo[0].stDevInfo.ViDev = 0; + // Wide Dynamic Range Mode + vi_config.astViInfo[0].stDevInfo.enWDRMode = WDR_MODE_NONE; + // Input Pipe Info + vi_config.astViInfo[0].stPipeInfo.aPipe[0] = video_pipe; // video pipe + vi_config.astViInfo[0].stPipeInfo.aPipe[1] = snap_pipe; // snap pipe + vi_config.astViInfo[0].stPipeInfo.aPipe[2] = -1; + vi_config.astViInfo[0].stPipeInfo.aPipe[3] = -1; + vi_config.astViInfo[0].stPipeInfo.enMastPipeMode = VI_OFFLINE_VPSS_OFFLINE; + // 设置 VI 通道信息 + vi_config.astViInfo[0].stChnInfo.ViChn = 0; + vi_config.astViInfo[0].stChnInfo.enPixFormat = + PIXEL_FORMAT_YVU_SEMIPLANAR_420; + vi_config.astViInfo[0].stChnInfo.enDynamicRange = DYNAMIC_RANGE_SDR8; + vi_config.astViInfo[0].stChnInfo.enVideoFormat = VIDEO_FORMAT_LINEAR; + vi_config.astViInfo[0].stChnInfo.enCompressMode = ISP_COMPRESS_MODE; + // 设置 VI 抓拍信息 + vi_config.astViInfo[0].stSnapInfo.bSnap = HI_TRUE; + vi_config.astViInfo[0].stSnapInfo.bDoublePipe = HI_TRUE; + vi_config.astViInfo[0].stSnapInfo.VideoPipe = video_pipe; + vi_config.astViInfo[0].stSnapInfo.SnapPipe = snap_pipe; + vi_config.astViInfo[0].stSnapInfo.enVideoPipeMode = VI_OFFLINE_VPSS_OFFLINE; + vi_config.astViInfo[0].stSnapInfo.enSnapPipeMode = VI_OFFLINE_VPSS_OFFLINE; + + // ****************************************** + // * Step2: VB & MPI Configuration and Init * + // ****************************************** + // Get sensor pic size type + ret = SAMPLE_COMM_VI_GetSizeBySensor( + vi_config.astViInfo[0].stSnsInfo.enSnsType, &vi_pic_size_type); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Get sensor picture size failed with %d", ret); + goto EXIT2; + } + // Convert pic type to struction + ret = SAMPLE_COMM_SYS_GetPicSize(vi_pic_size_type, &pic_size); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, + "Failure: Convert pic type failed with wrong pic type %d", + vi_pic_size_type); + goto EXIT2; + } + + memset(&vb_config, 0, sizeof(VB_CONFIG_S)); + // Set picture buffer + vb_config.u32MaxPoolCnt = 2; + // 获取一帧图片 buffer 的大小 + vb_config.astCommPool[0].u64BlkSize = COMMON_GetPicBufferSize( + pic_size.u32Width, pic_size.u32Height, ISP_PIXEL_FORMAT, DATA_BITWIDTH_8, + ISP_COMPRESS_MODE, DEFAULT_ALIGN); + vb_config.astCommPool[0].u32BlkCnt = VB_YUV_COUNT; + // 获取 raw buffer 的大小 + vb_config.astCommPool[1].u64BlkSize = VI_GetRawBufferSize( + pic_size.u32Width, pic_size.u32Height, PIXEL_FORMAT_RGB_BAYER_16BPP, + COMPRESS_MODE_NONE, DEFAULT_ALIGN); + vb_config.astCommPool[1].u32BlkCnt = VB_RGB_COUNT; + + // 初始化 VB + ret = + SAMPLE_COMM_SYS_InitWithVbSupplement(&vb_config, VB_SUPPLEMENT_JPEG_MASK); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, "Failure: Init VB and MPI failed with %#x!\n", ret); + goto EXIT2; + } + + // ******************* + // * Step3: VI Init * + // ******************* + ret = SAMPLE_COMM_VI_SetParam(&vi_config); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Set VI params failed"); + goto EXIT3; + } + + ISP_CTRL_PARAM_S stIspCtrlParam = {0}; + memset(&stIspCtrlParam, 0, sizeof(stIspCtrlParam)); + ret = HI_MPI_ISP_GetCtrlParam(vi_config.astViInfo[0].stPipeInfo.aPipe[0], + &stIspCtrlParam); + stIspCtrlParam.u32StatIntvl = frame_rate / 30; + ret = HI_MPI_ISP_SetCtrlParam(vi_config.astViInfo[0].stPipeInfo.aPipe[0], + &stIspCtrlParam); + + ret = SAMPLE_COMM_VI_StartVi(&vi_config); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, "Failure: Start VI failed with %#x!\n", ret); + goto EXIT3; + } + + // Init Success + is_isp_working = HI_TRUE; + zlog_debug(log_isp, "Success: ISP Init Finish..."); + return HI_SUCCESS; + + // Failure Conditions +EXTI4: + SAMPLE_COMM_VI_StopVi(&vi_config); +EXIT3: + SAMPLE_COMM_SYS_Exit(); +EXIT2: +EXIT1: + + return ret; +} + +/** + * @brief Stop ISP and Exit + * + */ +void ispExit(void) { + if (is_isp_working) { + SAMPLE_COMM_VI_StopVi(&vi_config); + SAMPLE_COMM_SYS_Exit(); + is_isp_working = HI_FALSE; + } + zlog_info(log_isp, "Success: ISP exit successfully"); +} + +HI_BOOL ispCheck(void) { return is_isp_working; } + +SAMPLE_VI_CONFIG_S *ispGetViConfig(void) { return &vi_config; } + +PIC_SIZE_E ispGetViPicSizeType(void) { return vi_pic_size_type; } + +VI_PIPE ispGetVideoPipe(void) { + return vi_config.astViInfo[0].stSnapInfo.VideoPipe; +} + +VI_PIPE ispGetSnapPipe(void) { + return vi_config.astViInfo[0].stSnapInfo.SnapPipe; +} + +isp_vpss_config_t *ispGetVideoVpss(void) { return &vpss_video_config; } + +isp_vpss_chn_t *ispGetVideoVpssChn(void) { return &vpss_video_chn; } + +VENC_CHN ispGetVideoVencChn(void) { return venc_video_chn; } + +isp_vpss_config_t *ispGetSnapVpss(void) { return &vpss_snap_config; } + +isp_vpss_chn_t *ispGetSnapVpssChn(void) { return &vpss_snap_chn; } + +VENC_CHN ispGetSnapVencChn(void) { return venc_snap_chn; } + +SAMPLE_VO_CONFIG_S *ispGetVOConfig(void) { return &vo_config; } + +/** + * @brief Get Vpss Chn Attrbute + * + * @param vpss_chn_attr The struction of vpss chn attr + * @param vpss_chn The number of vpss chn + * @param pic_size The enum of picture size + * @return HI_S32 success or failure + */ +HI_S32 ispGetVpssChnAttr(VPSS_CHN_ATTR_S *vpss_chn_attr, VPSS_CHN vpss_chn, + PIC_SIZE_E pic_size) { + HI_S32 ret = HI_SUCCESS; + + // Convert pic size enum to struct + SIZE_S size; + ret = SAMPLE_COMM_SYS_GetPicSize(pic_size, &size); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Failed to get picture size"); + return ret; + } + + // Configuration + vpss_chn_attr->enChnMode = VPSS_CHN_MODE_USER; + vpss_chn_attr->u32Width = size.u32Width; + vpss_chn_attr->u32Height = size.u32Height; + vpss_chn_attr->enVideoFormat = VIDEO_FORMAT_LINEAR; + vpss_chn_attr->enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420; + vpss_chn_attr->enDynamicRange = DYNAMIC_RANGE_SDR8; + vpss_chn_attr->enCompressMode = COMPRESS_MODE_NONE; + vpss_chn_attr->stFrameRate.s32SrcFrameRate = -1; + vpss_chn_attr->stFrameRate.s32DstFrameRate = -1; + vpss_chn_attr->u32Depth = 1; + vpss_chn_attr->bMirror = HI_FALSE; + vpss_chn_attr->bFlip = HI_FALSE; + vpss_chn_attr->stAspectRatio.enMode = ASPECT_RATIO_NONE; + + return ret; +} + +/** + * @brief Get Vpss configuration + * + * @param vpss_grp_id + * @param vpss_config + * @param vpss_chn + * @param vpss_chn_num + * @param pic_size + * @param is_snap + * @return HI_S32 + */ +HI_S32 ispGetVpssConfig(VPSS_GRP vpss_grp_id, isp_vpss_config_t *vpss_config, + VPSS_CHN *vpss_chn, HI_U8 vpss_chn_num, + PIC_SIZE_E pic_size, HI_BOOL is_snap) { + HI_S32 ret = HI_SUCCESS; + + // Assert vpss group id less than 16 + if (vpss_grp_id > VPSS_MAX_GRP_NUM) { + zlog_error(log_isp, "Failure: VPSS group id should not over %d", + VPSS_MAX_GRP_NUM); + return HI_FAILURE; + } + zlog_debug(log_isp, "VPSS Group ID: %d", vpss_grp_id); + + // Assert vpss chn number less than 11 + if (vpss_chn_num > VPSS_MAX_CHN_NUM) { + zlog_error(log_isp, "Failure: chn number should not over %d", + VPSS_MAX_CHN_NUM); + return HI_FAILURE; + } + + // Convert pic size enum to struct + SIZE_S size; + ret = SAMPLE_COMM_SYS_GetPicSize(pic_size, &size); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Failed to get picture size"); + return ret; + } + + // Vpss Configuration + vpss_config->s32GrpId = vpss_grp_id; + vpss_config->stGrpAttr.u32MaxW = size.u32Width; + vpss_config->stGrpAttr.u32MaxH = size.u32Width; + vpss_config->stGrpAttr.enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420; + vpss_config->stGrpAttr.enDynamicRange = DYNAMIC_RANGE_SDR8; + vpss_config->stGrpAttr.stFrameRate.s32SrcFrameRate = -1; + vpss_config->stGrpAttr.stFrameRate.s32DstFrameRate = -1; + vpss_config->stGrpAttr.bNrEn = HI_TRUE; + vpss_config->stGrpAttr.stNrAttr.enNrType = + !is_snap ? VPSS_NR_TYPE_VIDEO : VPSS_NR_TYPE_SNAP; + vpss_config->stGrpAttr.stNrAttr.enCompressMode = COMPRESS_MODE_FRAME; + vpss_config->stGrpAttr.stNrAttr.enNrMotionMode = NR_MOTION_MODE_NORMAL; + + // Vpss Channel Configuration + for (HI_S32 i = 0; i < vpss_chn_num; i++) { + VPSS_CHN _vpss_chn = vpss_chn[i]; + if (_vpss_chn > VPSS_MAX_CHN_NUM) { + zlog_error( + log_isp, + "Failure: Failed to set vpss chn: chn number should not over %d", + VPSS_MAX_CHN_NUM); + return HI_FAILURE; + } + + // Enable Chn and set Chn Attr + vpss_config->abChnEnable[_vpss_chn] = HI_TRUE; + ret = ispGetVpssChnAttr(&vpss_config->astChnAttrs[_vpss_chn], _vpss_chn, + PIC_1080P); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Failed to set vpss chn"); + return HI_FAILURE; + } + } + + return ret; +} + +void ispPrintVpssErrorCode(void) { + zlog_info(log_isp, "HI_ERR_VPSS_NULL_PTR : 0x%x", + HI_ERR_VPSS_NULL_PTR); + zlog_info(log_isp, "HI_ERR_VPSS_NOTREADY : 0x%x", + HI_ERR_VPSS_NOTREADY); + zlog_info(log_isp, "HI_ERR_VPSS_INVALID_DEVID : 0x%x", + HI_ERR_VPSS_INVALID_DEVID); + zlog_info(log_isp, "HI_ERR_VPSS_INVALID_CHNID : 0x%x", + HI_ERR_VPSS_INVALID_CHNID); + zlog_info(log_isp, "HI_ERR_VPSS_EXIST : 0x%x", HI_ERR_VPSS_EXIST); + zlog_info(log_isp, "HI_ERR_VPSS_UNEXIST : 0x%x", + HI_ERR_VPSS_UNEXIST); + zlog_info(log_isp, "HI_ERR_VPSS_NOT_SUPPORT : 0x%x", + HI_ERR_VPSS_NOT_SUPPORT); + zlog_info(log_isp, "HI_ERR_VPSS_NOT_PERM : 0x%x", + HI_ERR_VPSS_NOT_PERM); + zlog_info(log_isp, "HI_ERR_VPSS_NOMEM : 0x%x", HI_ERR_VPSS_NOMEM); + zlog_info(log_isp, "HI_ERR_VPSS_NOBUF : 0x%x", HI_ERR_VPSS_NOBUF); + zlog_info(log_isp, "HI_ERR_VPSS_SIZE_NOT_ENOUGH : 0x%x", + HI_ERR_VPSS_SIZE_NOT_ENOUGH); + zlog_info(log_isp, "HI_ERR_VPSS_ILLEGAL_PARAM : 0x%x", + HI_ERR_VPSS_ILLEGAL_PARAM); + zlog_info(log_isp, "HI_ERR_VPSS_BUSY : 0x%x", HI_ERR_VPSS_BUSY); + zlog_info(log_isp, "HI_ERR_VPSS_BUF_EMPTY : 0x%x", + HI_ERR_VPSS_BUF_EMPTY); +} \ No newline at end of file diff --git a/src/modules/isp/isp.h b/src/modules/isp/isp.h new file mode 100644 index 0000000..063b9f3 --- /dev/null +++ b/src/modules/isp/isp.h @@ -0,0 +1,79 @@ +#ifndef __ISP_H__ +#define __ISP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "hi_comm_video.h" +#include "hi_comm_vpss.h" +#include "hi_common.h" +#include "hi_type.h" +#include "sample_comm.h" + +#include "zlog.h" + +// zlog +extern zlog_category_t *log_isp; + +extern const PIXEL_FORMAT_E ISP_PIXEL_FORMAT2; + +extern const COMPRESS_MODE_E ISP_COMPRESS_MODE; + +typedef struct IspVpssConfig_S { + VPSS_GRP s32GrpId; + VPSS_GRP_ATTR_S stGrpAttr; + HI_BOOL abChnEnable[VPSS_MAX_PHY_CHN_NUM]; + VPSS_CHN_ATTR_S astChnAttrs[VPSS_MAX_PHY_CHN_NUM]; +} isp_vpss_config_t; + +typedef struct IspVpssChn_S { + VPSS_CHN *as32VpssChn; + HI_U8 u8VpssChnNum; +} isp_vpss_chn_t; + +HI_S32 ispInit(VI_PIPE video_pipe, VI_PIPE snap_pipe); + +void ispExit(void); + +HI_BOOL ispCheck(void); + +// Get ISP Configuration Struction +SAMPLE_VI_CONFIG_S *ispGetViConfig(void); + +PIC_SIZE_E ispGetViPicSizeType(void); + +VI_PIPE ispGetVideoPipe(void); + +VI_PIPE ispGetSnapPipe(void); + +isp_vpss_config_t *ispGetVideoVpss(void); + +isp_vpss_chn_t *ispGetVideoVpssChn(void); + +VENC_CHN ispGetVideoVencChn(void); + +isp_vpss_config_t *ispGetSnapVpss(void); + +isp_vpss_chn_t *ispGetSnapVpssChn(void); + +VENC_CHN ispGetSnapVencChn(void); + +SAMPLE_VO_CONFIG_S *ispGetVOConfig(void); + +// Set Basic ISP Config +HI_S32 ispGetVpssChnAttr(VPSS_CHN_ATTR_S *vpss_chn_attr, VPSS_CHN vpss_chn, + PIC_SIZE_E pic_size); + +HI_S32 ispGetVpssConfig(VPSS_GRP vpss_grp_id, isp_vpss_config_t *vpss_config, + VPSS_CHN *vpss_chn, HI_U8 vpss_chn_num, + PIC_SIZE_E pic_size, HI_BOOL is_snap); + +// Print Error Code +void ispPrintVpssErrorCode(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/modules/isp/snap.c b/src/modules/isp/snap.c new file mode 100644 index 0000000..9befe71 --- /dev/null +++ b/src/modules/isp/snap.c @@ -0,0 +1,249 @@ +#include "snap.h" +#include "hi_type.h" +#include "isp.h" + +#include "hi_comm_snap.h" +#include "hi_comm_video.h" +#include "hi_common.h" +#include "mpi_snap.h" +#include "sample_comm.h" + +#include + +extern zlog_category_t *log_isp; + +static HI_BOOL is_isp_snap_working = HI_FALSE; + +HI_S32 ispStartSnap(void) { + HI_S32 ret = HI_SUCCESS; + + // VI Info + SAMPLE_VI_CONFIG_S *vi_config = ispGetViConfig(); + VI_PIPE snap_pipe = ispGetSnapPipe(); + VI_CHN vi_chn = vi_config->astViInfo[0].stChnInfo.ViChn; + PIC_SIZE_E vi_pic_size_type = ispGetViPicSizeType(); + + // VPSS Info + isp_vpss_config_t *vpss_snap_config = ispGetSnapVpss(); + isp_vpss_chn_t *vpss_snap_chn = ispGetSnapVpssChn(); + + // Venc Info + VENC_CHN venc_snap_chn = ispGetSnapVencChn(); + +#ifdef DEBUG + ispPrintVpssErrorCode(); +#endif // DEBUG + + // ******************************* + // * Step1: VPSS Config and Init * + // ******************************* + // Get VPSS config + ret = ispGetVpssConfig( + (VPSS_GRP)1, vpss_snap_config, vpss_snap_chn->as32VpssChn, + vpss_snap_chn->u8VpssChnNum, vi_pic_size_type, HI_TRUE); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Failed to get Snap VPSS config"); + goto EXIT1; + } + // Start VPSS + ret = SAMPLE_COMM_VPSS_Start( + vpss_snap_config->s32GrpId, vpss_snap_config->abChnEnable, + &vpss_snap_config->stGrpAttr, vpss_snap_config->astChnAttrs); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, "Failure: Start Snap VPSS group %d failed with %#x!\n", + vpss_snap_config->s32GrpId, ret); + goto EXIT1; + } + + // ************************** + // * Step2: Bind VI -> VPSS * + // ************************** + ret = SAMPLE_COMM_VI_Bind_VPSS(snap_pipe, vi_chn, vpss_snap_config->s32GrpId); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, + "Failure: VI Video Pipe bind Snap VPSS failed with %#x!\n", ret); + goto EXIT2; + } + + // ************************* + // * Step3: Star Snap Venc * + // ************************* + SIZE_S vi_pic_size; + ret = SAMPLE_COMM_SYS_GetPicSize(vi_pic_size_type, &vi_pic_size); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, + "Failure: Convert pic type failed with wrong pic type %d", + vi_pic_size_type); + goto EXIT2; + } + zlog_debug(log_isp, "Debug: VI pic size type = %d", vi_pic_size_type); + // Start snap venc + ret = SAMPLE_COMM_VENC_SnapStart(venc_snap_chn, &vi_pic_size, HI_TRUE); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Venc Snap Start failed with %#x\n", ret); + goto EXIT2; + } + + // ********************************* + // * Step4: Bind VPSS -> Snap Venc * + // ********************************* + ret = SAMPLE_COMM_VPSS_Bind_VENC( + vpss_snap_config->s32GrpId, vpss_snap_chn->as32VpssChn[0], venc_snap_chn); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Venc bind Vpss failed with %#x!\n", ret); + goto EXIT3; + } + + // *************************** + // * Step4: Enable Snap Pipe * + // *************************** + SNAP_ATTR_S stSnapAttr; + stSnapAttr.enSnapType = SNAP_TYPE_NORMAL; + stSnapAttr.bLoadCCM = HI_TRUE; + stSnapAttr.stNormalAttr.u32FrameCnt = 1; + stSnapAttr.stNormalAttr.u32RepeatSendTimes = 1; + stSnapAttr.stNormalAttr.bZSL = HI_FALSE; + ret = HI_MPI_SNAP_SetPipeAttr(snap_pipe, &stSnapAttr); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: HI_MPI_SNAP_SetPipeAttr failed with %#x!\n", + ret); + goto EXIT4; + } + ret = HI_MPI_SNAP_EnablePipe(snap_pipe); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: HI_MPI_SNAP_EnablePipe failed with %#x!\n", + ret); + goto EXIT4; + } + + // // ************* + // // * Test Snap * + // // ************* + // printf("=======press any key to trigger=====\n"); + // getchar(); + + // ret = HI_MPI_SNAP_TriggerPipe(snap_pipe); + // if (HI_SUCCESS != ret) { + // SAMPLE_PRT("HI_MPI_SNAP_TriggerPipe failed with %#x!\n", ret); + // } + + // ret = SAMPLE_COMM_VENC_SnapProcess( + // venc_snap_chn, stSnapAttr.stNormalAttr.u32FrameCnt, HI_TRUE, HI_TRUE); + // if (HI_SUCCESS != ret) { + // printf("%s: sanp process failed!\n", __FUNCTION__); + // } + + // printf("snap success!\n"); + + is_isp_snap_working = HI_TRUE; + + return ret; + +// Failure Conditions +EXIT5: + HI_MPI_SNAP_DisablePipe(snap_pipe); +EXIT4: + SAMPLE_COMM_VPSS_UnBind_VENC(vpss_snap_config->s32GrpId, + vpss_snap_chn->as32VpssChn[0], venc_snap_chn); +EXIT3: + SAMPLE_COMM_VENC_SnapStop(venc_snap_chn); +EXIT2: + SAMPLE_COMM_VPSS_Stop(vpss_snap_config->s32GrpId, + vpss_snap_config->abChnEnable); +EXIT1: + ispExit(); + + return ret; +} + +void ispStopSnap(void) { + // VI Info + VI_PIPE snap_pipe = ispGetSnapPipe(); + + // VPSS Info + isp_vpss_config_t *vpss_snap_config = ispGetSnapVpss(); + isp_vpss_chn_t *vpss_snap_chn = ispGetSnapVpssChn(); + + // Venc Info + VENC_CHN venc_snap_chn = ispGetSnapVencChn(); + + HI_MPI_SNAP_DisablePipe(snap_pipe); + SAMPLE_COMM_VPSS_UnBind_VENC(vpss_snap_config->s32GrpId, + vpss_snap_chn->as32VpssChn[0], venc_snap_chn); + SAMPLE_COMM_VENC_SnapStop(venc_snap_chn); + SAMPLE_COMM_VPSS_Stop(vpss_snap_config->s32GrpId, + vpss_snap_config->abChnEnable); + ispExit(); + is_isp_snap_working = HI_FALSE; +} + +HI_BOOL ispCheckSnap(void) { return is_isp_snap_working; } + +HI_S32 ispSnapInstantly(void) { + HI_S32 ret = HI_SUCCESS; + + // VI Info + VI_PIPE snap_pipe = ispGetSnapPipe(); + + // Venc Info + VENC_CHN venc_snap_chn = ispGetSnapVencChn(); + + if (!is_isp_snap_working) { + zlog_warn(log_isp, "Snap Failed, Because ISP Snap Module Not Work"); + return HI_FAILURE; + } + + zlog_info(log_isp, "Ready to snap"); + + ret = HI_MPI_SNAP_TriggerPipe(snap_pipe); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: HI_MPI_SNAP_TriggerPipe failed with %#x!\n", + ret); + return HI_FAILURE; + } + + ret = SAMPLE_COMM_VENC_SnapProcess(venc_snap_chn, 1, HI_TRUE, HI_TRUE); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: %s: sanp process failed!\n", __FUNCTION__); + return HI_FAILURE; + } + + zlog_info(log_isp, "Success: Snap successfully"); + return HI_SUCCESS; +} + +HI_S32 ispSnapByPressKey(void) { + HI_S32 ret = HI_SUCCESS; + + // VI Info + VI_PIPE snap_pipe = ispGetSnapPipe(); + + // Venc Info + VENC_CHN venc_snap_chn = ispGetSnapVencChn(); + + if (!is_isp_snap_working) { + zlog_warn(log_isp, "Snap Failed, Because ISP Snap Module Not Work"); + return HI_FAILURE; + } + + zlog_info(log_isp, "Ready to snap"); + zlog_info(log_isp, "=======press any key to trigger======="); + getchar(); + + ret = HI_MPI_SNAP_TriggerPipe(snap_pipe); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: HI_MPI_SNAP_TriggerPipe failed with %#x!\n", + ret); + return HI_FAILURE; + } + + ret = SAMPLE_COMM_VENC_SnapProcess(venc_snap_chn, 1, HI_TRUE, HI_TRUE); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: %s: sanp process failed!\n", __FUNCTION__); + return HI_FAILURE; + } + + zlog_info(log_isp, "Success: Snap successfully"); + + return HI_SUCCESS; +} diff --git a/src/modules/isp/snap.h b/src/modules/isp/snap.h new file mode 100644 index 0000000..ebabdcc --- /dev/null +++ b/src/modules/isp/snap.h @@ -0,0 +1,26 @@ +#ifndef __SNAP_H__ +#define __SNAP_H__ + + +#ifdef __cplusplus +extern "C"{ +#endif + +#include "hi_type.h" + +HI_S32 ispStartSnap(void); + +void ispStopSnap(void); + +HI_BOOL ispCheckSnap(void); + +HI_S32 ispSnapByPressKey(void); + +HI_S32 ispSnapInstantly(void); + + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/modules/isp/video.c b/src/modules/isp/video.c new file mode 100644 index 0000000..8e0ae1b --- /dev/null +++ b/src/modules/isp/video.c @@ -0,0 +1,157 @@ +#include "video.h" +#include "isp.h" + +#include "hi_common.h" +#include "hi_type.h" +#include "sample_comm.h" +#include + +extern zlog_category_t *log_isp; + +static HI_BOOL is_isp_video_working = HI_FALSE; + +HI_S32 ispStartVideo(void) { + HI_S32 ret = HI_SUCCESS; + + // VI Info + SAMPLE_VI_CONFIG_S *vi_config = ispGetViConfig(); + VI_PIPE video_pipe = ispGetVideoPipe(); + VI_CHN vi_chn = vi_config->astViInfo[0].stChnInfo.ViChn; + PIC_SIZE_E vi_pic_size_type = ispGetViPicSizeType(); + + // VPSS Info + isp_vpss_config_t *vpss_video_config = ispGetVideoVpss(); + isp_vpss_chn_t *vpss_video_chn = ispGetVideoVpssChn(); + + // VO Info + SAMPLE_VO_CONFIG_S *vo_config = ispGetVOConfig(); + + // Check ISP working status + if (!ispCheck()) { + zlog_error(log_isp, "Failure: ISP not working!"); + return HI_FAILURE; + } + +#ifdef DEBUG + ispPrintVpssErrorCode(); +#endif // DEBUG + + // ******************************* + // * Step1: VPSS Config and Init * + // ******************************* + // Get VPSS config + ret = ispGetVpssConfig( + (VPSS_GRP)0, vpss_video_config, vpss_video_chn->as32VpssChn, + vpss_video_chn->u8VpssChnNum, vi_pic_size_type, HI_FALSE); + // SIZE_S stSize; + // SAMPLE_COMM_SYS_GetPicSize(vi_pic_size_type, &stSize); + // ret = VPSS_GetParams((SAMPLE_VPSS_CONFIG_S *)vpss_video_config, &stSize, + // HI_FALSE); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Failed to get Video VPSS config"); + goto EXIT1; + } + // Start VPSS + ret = SAMPLE_COMM_VPSS_Start( + vpss_video_config->s32GrpId, vpss_video_config->abChnEnable, + &vpss_video_config->stGrpAttr, vpss_video_config->astChnAttrs); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, "Failure: Start Video VPSS group %d failed with %#x!\n", + vpss_video_config->s32GrpId, ret); + goto EXIT1; + } + + // ************************** + // * Step2: Bind VI -> VPSS * + // ************************** + ret = + SAMPLE_COMM_VI_Bind_VPSS(video_pipe, vi_chn, vpss_video_config->s32GrpId); + if (ret != HI_SUCCESS) { + zlog_error(log_isp, "Failure: VI Video Pipe bind Video VPSS failed with %#x!\n", + ret); + goto EXIT2; + } + + // ******************************* + // * Step3: VO Config and Init * + // ******************************* + // VO config + ret = SAMPLE_COMM_VO_GetDefConfig(vo_config); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Get VO config failed"); + goto EXIT3; + } + vo_config->enDstDynamicRange = DYNAMIC_RANGE_SDR8; + vo_config->enVoIntfType = VO_INTF_HDMI; + vo_config->enPicSize = vi_pic_size_type; + + // Start Vo + ret = SAMPLE_COMM_VO_StartVO(vo_config); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: Start vo failed. s32Ret: 0x%x !", ret); + goto EXIT3; + } + + // ******************************** + // * Step4: Bind VPSS -> VO(HDMI) * + // ******************************** + ret = SAMPLE_COMM_VPSS_Bind_VO(vpss_video_config->s32GrpId, + vpss_video_chn->as32VpssChn[0], + vo_config->VoDev, (VO_CHN)0); + if (HI_SUCCESS != ret) { + zlog_error(log_isp, "Failure: VO bind vpss failed. s32Ret: 0x%x !\n", ret); + goto EXIT4; + } + + is_isp_video_working = HI_TRUE; + zlog_debug(log_isp, "Success: ISP Video Module Start Successfully!"); + return ret; + +// Failure Conditions +EXIT5: + SAMPLE_COMM_VPSS_UnBind_VO(vpss_video_config->s32GrpId, + vpss_video_chn->as32VpssChn[0], vo_config->VoDev, + (VO_CHN)0); +EXIT4: + SAMPLE_COMM_VO_StopVO(vo_config); +EXIT3: + SAMPLE_COMM_VI_UnBind_VPSS(video_pipe, vi_chn, vpss_video_config->s32GrpId); +EXIT2: + SAMPLE_COMM_VPSS_Stop(vpss_video_config->s32GrpId, + vpss_video_config->abChnEnable); +EXIT1: + ispExit(); + + return ret; +} + +void ispStopVideo(void) { + // VI Info + SAMPLE_VI_CONFIG_S *vi_config = ispGetViConfig(); + VI_PIPE video_pipe = ispGetVideoPipe(); + VI_CHN vi_chn = vi_config->astViInfo[0].stChnInfo.ViChn; + + // VPSS Info + isp_vpss_config_t *vpss_video_config = ispGetVideoVpss(); + isp_vpss_chn_t *vpss_video_chn = ispGetVideoVpssChn(); + + // VO Info + SAMPLE_VO_CONFIG_S *vo_config = ispGetVOConfig(); + + // Stop VPSS and VO + SAMPLE_COMM_VPSS_UnBind_VO(vpss_video_config->s32GrpId, + vpss_video_chn->as32VpssChn[0], vo_config->VoDev, + (VO_CHN)0); + SAMPLE_COMM_VO_StopVO(vo_config); + SAMPLE_COMM_VI_UnBind_VPSS(video_pipe, vi_chn, vpss_video_config->s32GrpId); + SAMPLE_COMM_VPSS_Stop(vpss_video_config->s32GrpId, + vpss_video_config->abChnEnable); + // Stop isp + if (ispCheck()) { + ispExit(); + } + + is_isp_video_working = HI_FALSE; +} + +HI_BOOL ispCheckVideo(void) { return is_isp_video_working; } \ No newline at end of file diff --git a/src/modules/isp/video.h b/src/modules/isp/video.h new file mode 100644 index 0000000..cdb4ed1 --- /dev/null +++ b/src/modules/isp/video.h @@ -0,0 +1,20 @@ +#ifndef __VIDEO_H__ +#define __VIDEO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "hi_type.h" + +HI_S32 ispStartVideo(void); + +void ispStopVideo(void); + +HI_BOOL ispCheckVideo(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/modules/util.h b/src/modules/util.h index 53db0d3..125c636 100755 --- a/src/modules/util.h +++ b/src/modules/util.h @@ -38,13 +38,13 @@ extern "C" { #define RGB888_WHITE ((uint32_t)0xFFFFFF) #define RGB888_BLACK ((uint32_t)0x000000) -#ifndef SAMPLE_PRT -#define SAMPLE_PRT(fmt...) \ - do { \ - printf("[%s]-%d: ", __FUNCTION__, __LINE__); \ - printf(fmt); \ - } while (0) -#endif +// #ifndef SAMPLE_PRT +// #define SAMPLE_PRT(fmt...) \ +// do { \ +// printf("[%s]-%d: ", __FUNCTION__, __LINE__); \ +// printf(fmt); \ +// } while (0) +// #endif typedef struct { int xmin;