int main(int argc, char const *argv[]) { VI_EXT_CHN_ATTR_S stExtChnAttr; VB_BLK hBlock; VB_POOL hPool = VB_INVALID_POOLID; HI_S32 s32Ret = HI_SUCCESS; HI_U32 u32BlkSize, u32DstBlkSize; VIDEO_FRAME_INFO_S FrameInfo; IVE_HANDLE IveHandle; IVE_SRC_IMAGE_S stSrc; IVE_DST_IMAGE_S stDst; IVE_CSC_CTRL_S stCscCtrl; HI_BOOL bInstant = HI_TRUE; HI_BOOL bFinish; IplImage *iplImage; FILE *fp,*fp2; int k=1; #if 0 memset(&stVbConf, 0, sizeof(VB_CONF_S)); stVbConf.u32MaxPoolCnt = 128; SAMPLE_COMM_VI_GetSizeBySensor(&enSize); u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm, enSize, SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH); stVbConf.astCommPool[0].u32BlkSize = u32BlkSize; stVbConf.astCommPool[0].u32BlkCnt = 12; s32Ret = SAMPLE_COMM_SYS_Init(&stVbConf); if (HI_SUCCESS != s32Ret) { printf("system init failed with err code %#x!\n", s32Ret ); } stViConfig.enViMode = SENSOR_TYPE; stViConfig.enRotate = ROTATE_NONE; stViConfig.enNorm = VIDEO_ENCODING_MODE_AUTO; stViConfig.enViChnSet = VI_CHN_SET_NORMAL; s32Ret = SAMPLE_COMM_VI_StartVi(&stViConfig); if (HI_SUCCESS != s32Ret) { printf("start vi failed with err code %#x!\n", s32Ret); // goto END_1; } #endif iplImage = cvCreateImageHeader(cvSize(WIDTH,HEIGHT), IPL_DEPTH_8U, 3); stExtChnAttr.enPixFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420; stExtChnAttr.s32BindChn = ViChn; stExtChnAttr.stDestSize.u32Width = WIDTH; stExtChnAttr.stDestSize.u32Height = HEIGHT; stExtChnAttr.s32DstFrameRate = -1; stExtChnAttr.s32SrcFrameRate = -1; stExtChnAttr.enCompressMode = COMPRESS_MODE_NONE; HI_MPI_VI_DisableChn(ExtChn); s32Ret = HI_MPI_VI_SetExtChnAttr(ExtChn, &stExtChnAttr); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VI_SetExtChnAttr failed with err code %#x\n", s32Ret); return -1; } s32Ret = HI_MPI_VI_SetFrameDepth(ExtChn, 1); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VI_SetFrameDepth failed with err code %#x\n", s32Ret); return -1; } stSrc.enType = IVE_IMAGE_TYPE_YUV420SP; //stDst.enType = IVE_IMAGE_TYPE_U8C3_PLANAR; stDst.enType = IVE_IMAGE_TYPE_U8C3_PACKAGE; stCscCtrl.enMode = IVE_CSC_MODE_PIC_BT709_YUV2RGB; #if 1 s32Ret = HI_MPI_VI_EnableChn(ExtChn); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VI_EnableChn failed with err code %#x\n", s32Ret); return -1; } #endif printf("begin\n"); det_t *pdet = det_open("bd_detect_trace.config"); while(k) { s32Ret = HI_MPI_VI_GetFrame(ExtChn, &FrameInfo, -1); if(HI_SUCCESS != s32Ret) { printf("HI_MPI_VI_GetFrame failed with err code %#x!\n",s32Ret); } printf("get frame!\n"); u32DstBlkSize = FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height * 3; s32Ret = HI_MPI_SYS_MmzAlloc_Cached(&stDst.u32PhyAddr[0],(void**) &stDst.pu8VirAddr[0], "user", HI_NULL, u32DstBlkSize); HI_MPI_SYS_MmzFlushCache(stDst.u32PhyAddr[0], (void *)stDst.pu8VirAddr[0], u32DstBlkSize); #if 0 stDst.u32PhyAddr[1] = stDst.u32PhyAddr[0] + FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height; stDst.u32PhyAddr[2] = stDst.u32PhyAddr[1] + FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height; stDst.pu8VirAddr[0] = (HI_U8*) HI_MPI_SYS_Mmap(stDst.u32PhyAddr[0], u32DstBlkSize); stDst.pu8VirAddr[1] = stDst.pu8VirAddr[0] + FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height; stDst.pu8VirAddr[2] = stDst.pu8VirAddr[1] + FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height; stDst.u16Stride[0] = FrameInfo.stVFrame.u32Stride[0]; stDst.u16Stride[1] = FrameInfo.stVFrame.u32Stride[0]; stDst.u16Stride[2] = FrameInfo.stVFrame.u32Stride[0]; stDst.u16Width = FrameInfo.stVFrame.u32Width; stDst.u16Height = FrameInfo.stVFrame.u32Height; #endif #if 1 stDst.pu8VirAddr[0] = (HI_U8*) HI_MPI_SYS_Mmap(stDst.u32PhyAddr[0], u32DstBlkSize); stDst.u16Stride[0] = FrameInfo.stVFrame.u32Stride[0]; stDst.u16Width = FrameInfo.stVFrame.u32Width; stDst.u16Height = FrameInfo.stVFrame.u32Height; #endif stSrc.u32PhyAddr[0] = FrameInfo.stVFrame.u32PhyAddr[0]; stSrc.u32PhyAddr[1] = FrameInfo.stVFrame.u32PhyAddr[1]; stSrc.u32PhyAddr[2] = FrameInfo.stVFrame.u32PhyAddr[2]; stSrc.pu8VirAddr[0] = (HI_U8*)HI_MPI_SYS_Mmap(FrameInfo.stVFrame.u32PhyAddr[0], u32DstBlkSize / 2); stSrc.pu8VirAddr[1] = stSrc.pu8VirAddr[0] + FrameInfo.stVFrame.u32Stride[0] * FrameInfo.stVFrame.u32Height; stSrc.pu8VirAddr[2] = stSrc.pu8VirAddr[1] + 1; stSrc.u16Stride[1] = FrameInfo.stVFrame.u32Stride[0]; stSrc.u16Stride[0] = FrameInfo.stVFrame.u32Stride[0]; stSrc.u16Stride[2] = 0; stSrc.u16Width = FrameInfo.stVFrame.u32Width; stSrc.u16Height = FrameInfo.stVFrame.u32Height; #if 0 for(k = 0; k < 720 * 576 * 3 / 2;k++) *(stSrc.pu8VirAddr[0] + k) = 130; k = 0; #endif s32Ret = HI_MPI_IVE_CSC(&IveHandle, &stSrc, &stDst, &stCscCtrl, bInstant); if(s32Ret != HI_SUCCESS) { printf("HI_MPI_IVE_CSC failed with error code %#x\n",s32Ret); continue; } s32Ret = HI_MPI_IVE_Query(IveHandle, &bFinish, HI_TRUE); if(s32Ret != HI_SUCCESS) { printf("HI_MPI_IVE_Query failed with error code %#x\n",s32Ret); continue; } #if 0 fp = fopen("image.rgb","wb"); fwrite(stDst.pu8VirAddr[0], WIDTH * HEIGHT * 3, 1, fp); fclose(fp); fp2 = fopen("image2.yuv","wb"); fwrite(stSrc.pu8VirAddr[0], WIDTH * HEIGHT * 3 / 2, 1, fp2); fclose(fp2); #endif //printf("u16Width:%d,u16Height:%d\n",stDst.u16Width,stDst.u16Height); //printf("widthStep:%d\n",iplImage->widthStep); //cvSetData(iplImage, stDst.pu8VirAddr[0], iplImage->widthStep); //cvSaveImage("saveImage.jpg",iplImage); char *str = det_detect(pdet, HEIGHT, WIDTH, (char *)stDst.pu8VirAddr[0]); printf("%s\n",str); HI_MPI_SYS_Munmap(stDst.pu8VirAddr[0], u32DstBlkSize); HI_MPI_SYS_Munmap(stSrc.pu8VirAddr[0], u32DstBlkSize / 2); HI_MPI_SYS_MmzFree(stDst.u32PhyAddr[0], NULL); HI_MPI_VI_ReleaseFrame(ExtChn, &FrameInfo); } det_close(pdet); }
IMP_S32 PEA_ALGO_PROCESS() { IMP_S32 nFrmNum = 0; VIDEO_FRAME_INFO_S stFrame; VI_DEV ViDev = 0; VI_CHN ViChn =0; OBJ_S stObj; LIB_INFO_S stLibInfo; IMP_HANDLE hIMP = &stObj; MEM_SET_S stMems; YUV_IMAGE422_S stImage; RESULT_S stResult; IMP_S32 s32Width, s32Height; IMP_S32 bRet = 0; s32Width = IMG_WIDTH; s32Height = IMG_HEIGHT; IMP_YUVImage422Create( &stImage, s32Width, s32Height, NULL ); // 获取库信息 IMP_GetAlgoLibInfo( hIMP, &stLibInfo ); // 获取内存需求 stMems.u32ImgW = s32Width; stMems.u32ImgH = s32Height; IMP_GetMemReq( hIMP, &stMems ); IMP_MemsAlloc( &stMems ); printf("w:%d, h:%d\n", s32Width, s32Height); if(IMP_Create( hIMP, &stMems ) != IMP_STATUS_OK) { printf("IMP_Create Error!\n"); exit(0); } #if defined(IMP_DEBUG_PRINT) printf("IMP_Create ok!\n"); #endif // 配置参数 IMP_ParaConfig( hIMP ); //IMP_PARA_Config( hIMP,s32Width,s32Height ); IMP_Start( hIMP ); #if defined(IMP_DEBUG_PRINT) printf("IMP_Start ok!\n"); #endif sleep(2); while(1) { if(master_thread_init_ok == 0) continue; //Get Image From Hisi Interface if (HI_MPI_VI_GetFrame(ViDev, ViChn, &stFrame)) { printf("HI_MPI_VI_GetFrame err, vi(%d,%d)\n", ViDev, ViChn); continue; } struct timeval t1, t2; gettimeofday(&t1, NULL); IMP_HiImageConvertToYUV422Image(&stFrame.stVFrame,&stImage); HI_MPI_VI_ReleaseFrame(ViDev, ViChn, &stFrame); // Process Image // IMP_ProcessImage( hIMP, &stImage ); // Get Algo Result // IMP_GetResults( hIMP, &stResult ); gettimeofday(&t2, NULL); printf("out:%d ms\n", (t2.tv_usec - t1.tv_usec) / 1000); printf("\n--------------------------\n"); #ifdef SHOW_DEBUG_INFO IMP_ShowDebugInfo(&stResult); #endif pthread_mutex_lock(&mut); gstPeaResult = stResult; pthread_mutex_unlock(&mut); usleep(100000); } IMP_Stop( hIMP, &stResult ); IMP_Release( hIMP ); IMP_MemsFree( &stMems ); IMP_YUVImage422Destroy( &stImage, NULL ); return bRet; }
HI_S32 SAMPLE_VIO_TDE_1Screen_VoVGA() { VB_CONF_S stVbConf = {0}; HI_S32 s32ViChnTotal = 1; HI_S32 s32VoChnTotal = 1; VO_DEV VoDev; VO_CHN VoChn; VI_DEV ViDev; VI_CHN ViChn; VI_PUB_ATTR_S stViDevAttr; VI_CHN_ATTR_S stViChnAttr; VO_PUB_ATTR_S stVoDevAttr; VO_VIDEO_LAYER_ATTR_S stVideoLayerAttr; VIDEO_FRAME_INFO_S stFrame; HI_S32 s32Ret = HI_SUCCESS; HI_S32 u32PhyAddr; stVbConf.astCommPool[0].u32BlkSize = 704 * 576 * 2; stVbConf.astCommPool[0].u32BlkCnt = 20; stVbConf.astCommPool[1].u32BlkSize = 384 * 288 * 2; stVbConf.astCommPool[1].u32BlkCnt = 20; if (HI_SUCCESS != SAMPLE_InitMPP(&stVbConf)) { return -1; } /* Config VI to input standard-definition video */ ViDev = 0; SAMPLE_GetViCfg_SD(PIC_D1, &stViDevAttr, &stViChnAttr); // SAMPLE_GetViCfg_SD(PIC_CIF, &stViDevAttr, &stViChnAttr); s32Ret = SAMPLE_StartViByChn(s32ViChnTotal, &stViDevAttr, &stViChnAttr); if (HI_SUCCESS != s32Ret) { return HI_FAILURE; } /* display VGA video on vo HD divice. */ VoDev = VO_DEV_HD; SAMPLE_GetVoCfg_VGA_800x600(&stVoDevAttr, &stVideoLayerAttr); s32Ret = SAMPLE_StartVo(s32VoChnTotal, VoDev, &stVoDevAttr, &stVideoLayerAttr); if (HI_SUCCESS != s32Ret) { return HI_FAILURE; } printf("start VI to VO preview \n"); master_thread_init_ok = 1; sleep(3); /** 1. 打开TDE */ HI_TDE2_Open(); /** 2. 创建Y U V OSD surface */ g_stOsdYSurface.enColorFmt = TDE2_COLOR_FMT_ARGB1555; g_stOsdYSurface.bYCbCrClut = 1; g_stOsdYSurface.bAlphaMax255 = HI_TRUE; g_stOsdYSurface.enColorSpaceConv = TDE2_ITU_R_BT601_VIDEO; g_stOsdUSurface.enColorFmt = TDE2_COLOR_FMT_ARGB1555; g_stOsdUSurface.bYCbCrClut = 0; g_stOsdUSurface.bAlphaMax255 = HI_TRUE; g_stOsdUSurface.enColorSpaceConv = TDE2_ITU_R_BT601_VIDEO; g_stOsdVSurface.enColorFmt = TDE2_COLOR_FMT_ARGB1555; g_stOsdVSurface.bYCbCrClut = 1; g_stOsdVSurface.bAlphaMax255 = HI_TRUE; g_stOsdVSurface.enColorSpaceConv = TDE2_ITU_R_BT601_VIDEO; while(1) { TGT_SET_S *target_set = &gstPeaResult.stTargetSet; EVT_SET_S *event_set = &gstPeaResult.stEventSet; IMP_S32 s32TargetNum = target_set->s32TargetNum; /** 获取图像 */ if (HI_MPI_VI_GetFrame(ViDev, 0, &stFrame)) { printf("HI_MPI_VI_GetFrame err, vi(%d,%d)\n", ViDev, 0); return -1; } if(s32TargetNum > 0 ) { TDE_HANDLE s32Handle; /* 3. 开始TDE任务 */ s32Handle = HI_TDE2_BeginJob(); if(HI_ERR_TDE_INVALID_HANDLE == s32Handle) { printf("start job failed!\n"); continue; } /** 4. 配置YUV Surface数据 长度、宽度、stride、物理地址等 */ u32PhyAddr = stFrame.stVFrame.u32PhyAddr[0]; g_stOsdYSurface.u32Width = stFrame.stVFrame.u32Width; g_stOsdYSurface.u32Height = stFrame.stVFrame.u32Height; g_stOsdYSurface.u32Stride = stFrame.stVFrame.u32Stride[0]; g_stOsdYSurface.u32PhyAddr = u32PhyAddr; g_stOsdUSurface.u32Width = stFrame.stVFrame.u32Width>>1; g_stOsdUSurface.u32Height = stFrame.stVFrame.u32Height>>1; g_stOsdUSurface.u32Stride = stFrame.stVFrame.u32Stride[1]; g_stOsdUSurface.u32PhyAddr = stFrame.stVFrame.u32PhyAddr[1]+1; g_stOsdVSurface = g_stOsdUSurface; g_stOsdVSurface.u32PhyAddr = stFrame.stVFrame.u32PhyAddr[1]; /** 5. 通过TDE绘制修改YUV数据显示PEA数据结果 TDE只支持绘制矩形区域、直线等*/ IMP_TDE_DrawPeaResult( s32Handle, &g_stOsdYSurface,&g_stOsdVSurface, &gstPeaResult,IMP_D1,IMP_QCIF ); /** 6. 直接修改YUV数据显示PEA数据结果 绘制轨迹线、用户配置的规则信息等*/ IMP_DrawPeaResult( &stFrame.stVFrame, &gstPeaResult,IMP_D1,IMP_QCIF ); /** 7. 结束TDE任务 */ s32Ret = HI_TDE2_EndJob(s32Handle, HI_FALSE, HI_TRUE, 10); if(s32Ret < 0) { printf("Line:%d,HI_TDE2_EndJob failed,ret=0x%x!\n", __LINE__, s32Ret); HI_TDE2_CancelJob(s32Handle); return ; } } /** 8. 将修改后的YUV数据送给VO设备 */ HI_MPI_VO_SendFrame( VoDev,0,&stFrame); /** 9. 释放图像 */ HI_MPI_VI_ReleaseFrame(ViDev, 0, &stFrame); }
static int vin_capture(int vin, FILE* bitmap_stream) { int ret = 0; int api_ret = SDK_FAILURE; if(vin < HI3520A_VIN_CH_BACKLOG_REF){ int frame_depth = 0; VIDEO_FRAME_INFO_S video_frame_info; SOC_CHECK(HI_MPI_VI_GetFrameDepth(vin, &frame_depth)); if(0 == frame_depth){ HI_MPI_VI_SetFrameDepth(vin, 1); } if(HI_SUCCESS == HI_MPI_VI_GetFrame(vin, &video_frame_info)){ if(PIXEL_FORMAT_YUV_SEMIPLANAR_420 == video_frame_info.stVFrame.enPixelFormat){ int i = 0, ii = 0; int const width = video_frame_info.stVFrame.u32Width; int const height = video_frame_info.stVFrame.u32Height; size_t const stride = video_frame_info.stVFrame.u32Stride[0]; size_t const yuv420_size = stride * height * 3 / 2; const uint8_t* yuv420_data = HI_MPI_SYS_Mmap(video_frame_info.stVFrame.u32PhyAddr[0], yuv420_size); // allocate the bitmap data size_t bitmap24_size = width * height * 3; uint8_t* bitmap24_data = alloca(bitmap24_size); uint8_t* bitmap24_pixel = bitmap24_data; SOC_TRACE("Frame(%d) [%dx%d] stride %d", vin, width, height, stride); if(yuv420_data){ int y, u, v, yy, vr, ug, vg, ub; int r, g, b; const uint8_t *py = (uint8_t*)(yuv420_data); const uint8_t *puv = (uint8_t*)(py + width * height); // yuv420 to rgb888 for(i = 0; i < height; ++i){ for(ii = 0; ii < width; ++ii){ y = py[0]; yy = y * 256; u = puv[1] - 128; ug = 88 * u; ub = 454 * u; v = puv[0] - 128; vg = 183 * v; vr = 359 * v; /////////////////////////////////// // convert r = (yy + vr) >> 8; g = (yy - ug - vg) >> 8; b = (yy + ub) >> 8; if(r < 0){ r = 0; } if(r > 255){ r = 255; } if(g < 0){ g = 0; } if(g > 255){ g = 255; } if(b < 0){ b = 0; } if(b > 255){ b = 255; } *bitmap24_pixel++ = (uint8_t)b; *bitmap24_pixel++ = (uint8_t)g; *bitmap24_pixel++ = (uint8_t)r; //SOC_TRACE("RGB[%3d,%3d,%3d] @ (%3d,%3d)", r, g, b, ii, i); /////////////////////////////////// ++py; if(0 != (ii % 2)){ // even puv += 2; } } if(0 != (i % 2)){ // try twice puv -= width; } } SOC_CHECK(HI_MPI_SYS_Munmap(yuv420_data, yuv420_size)); if(0 == fseek(bitmap_stream, 0, SEEK_SET)){ BIT_MAP_FILE_HEADER_t bmp_header; memset(&bmp_header, 0, sizeof(bmp_header)); bmp_header.type[0] = 'B'; bmp_header.type[1] = 'M'; bmp_header.file_size = sizeof(bmp_header) + bitmap24_size; bmp_header.reserved_zero = 0; bmp_header.off_bits = sizeof(bmp_header); bmp_header.info_size = 40; bmp_header.width = width; bmp_header.height = height; bmp_header.planes = 1; bmp_header.bit_count = 24; bmp_header.compression = 0; bmp_header.size_image = bitmap24_size; bmp_header.xpels_per_meter = 0; bmp_header.ypels_per_meter = 0; bmp_header.clr_used = 0; bmp_header.clr_important = 0; fwrite(&bmp_header, 1, sizeof(bmp_header), bitmap_stream); for(i = 0; i < height; ++i){ void* bitmap_offset = bitmap24_data + (height - i - 1) * width * 3; fwrite(bitmap_offset, 1, width * 3, bitmap_stream); } api_ret = SDK_SUCCESS; } } } SOC_CHECK(HI_MPI_VI_ReleaseFrame(vin, &video_frame_info)); }
void AnalyzePic() { HI_U32 s32Ret; VIDEO_FRAME_INFO_S FrameInfoA; struct timeval ustime; time_t timep; int fd; struct termios options; getCoord(); getLRPoi(); getFLenformfile(); int flag = 0; JSSetparam(X1, Y1, X2, Y2); //FILE *fb = fopen("/app/a.log","w+"); //FILE *fc = fopen("/app/b.log","w+"); while (1) { while (analyzeStart) { //获取图像帧 // time(&timep); //gettimeofday(&ustime,NULL); //printf("%ld\n",((long)ustime.tv_sec)*1000+(long)ustime.tv_usec/1000); // printf("%ld\n",timep); char *pImg; int size; char str1[20] = {}; char num_str[4] = {}; short YPoi = 0; char* str; s32Ret = HI_MPI_VI_GetFrameTimeOut(ExtChn, &FrameInfoA, 0); if (HI_SUCCESS != s32Ret) { printf("HI_MPI_VI_GetFrameTimeOut faileded with err code %#x!\n", s32Ret); } //分析图片 size = FrameInfoA.stVFrame.u32Stride[0] * FrameInfoA.stVFrame.u32Height; pImg = (char*)HI_MPI_SYS_Mmap(FrameInfoA.stVFrame.u32PhyAddr[0], size); //printf("%s\n",JSMyTest(pImg)); str = JSMyTest(pImg, str1); if (str != NULL) { //printf("%s\n", str); //fprintf(fc,"%s\n", str); //fflush(fc); strncpy(num_str, str, strstr(str, ",") - str); //YPoi = CalAngle(atoi(num_str)); //YPoi = -233 + 670 * (((X2 - X1) * (atoi(num_str)) / 100 + X1) / 720.0); //if (atoi(num_str) > 0 ) if (atoi(str) > 0 ) { //printf("LPoi=%d,RPoi=%d,RPoi-LPoi:%d\n",LPoi,RPoi,RPoi-LPoi); YPoi = (short)LPoi + ((short)RPoi - (short)LPoi) * (((X2 - X1) * (atoi(num_str)) / 100 + X1) / 720.0); //fprintf(fb,"%d\n", YPoi); //fflush(fb); SetPanTiltPoi(YSpeed, ZSpeed, YPoi, 0); if(flag == 1) { SetPanTiltFLen(FLen); flag = 0; } time(&timep); } else { if(time(NULL) - timep > 5) { SetPanTiltFLen(0); SetPanTiltPoi(0x99,0x99,0,0); flag = 1; } } } HI_MPI_SYS_Munmap(pImg, size); HI_MPI_VI_ReleaseFrame(ExtChn, &FrameInfoA); // usleep(50); //printf("============================================\n"); } } }