/****************************************************************************** * function : 1D1 User send pictures for H264 encode ******************************************************************************/ HI_S32 SAMPLE_VENC_1D1_USER_SEND_PICTURES(HI_VOID) { VB_CONF_S stVbConf; VENC_GRP VencGrp = 0; VENC_CHN VencChn = 0; PIC_SIZE_E enSize = PIC_D1; HI_S32 s32Ret = HI_SUCCESS; HI_U32 u32BlkSize; SIZE_S stSize; VB_POOL hPool = VB_INVALID_POOLID; FILE *pfp_img = HI_NULL; HI_U32 u32PicLStride = 0; HI_U32 u32PicCStride = 0; HI_U32 u32LumaSize = 0; HI_U32 u32ChrmSize = 0; HI_U32 u32Cnt = 0; HI_U32 u32ChnCnt = 1; /****************************************** step 1: init variable ******************************************/ memset(&stVbConf,0,sizeof(VB_CONF_S)); u32BlkSize = SAMPLE_COMM_SYS_CalcPicVbBlkSize(gs_enNorm,\ enSize, SAMPLE_PIXEL_FORMAT, SAMPLE_SYS_ALIGN_WIDTH); stVbConf.u32MaxPoolCnt = 128; /*ddr0 video buffer*/ stVbConf.astCommPool[0].u32BlkSize = u32BlkSize; stVbConf.astCommPool[0].u32BlkCnt = 10; memset(stVbConf.astCommPool[0].acMmzName,0, sizeof(stVbConf.astCommPool[0].acMmzName)); /****************************************** step 2: mpp system init. ******************************************/ s32Ret = SAMPLE_COMM_SYS_Init(&stVbConf); if (HI_SUCCESS != s32Ret) { SAMPLE_PRT("system init failed with %d!\n", s32Ret); goto END_VENC_USER_0; } /****************************************** step 3: open yuv file ******************************************/ if (pfp_img != HI_NULL) { fclose(pfp_img); pfp_img = HI_NULL; } pfp_img = fopen(SAMPLE_YUV_D1_FILEPATH, "rb" ); if (pfp_img == HI_NULL) { SAMPLE_PRT("Open yuv file failed!Check if the file %s exit\n",SAMPLE_YUV_D1_FILEPATH); goto END_VENC_USER_0; } /****************************************** step 4: create private pool on ddr0 ******************************************/ hPool = HI_MPI_VB_CreatePool( u32BlkSize, 10,NULL ); if (hPool == VB_INVALID_POOLID) { SAMPLE_PRT("HI_MPI_VB_CreatePool failed! \n"); goto END_VENC_USER_1; } /****************************************** step 5: encode process ******************************************/ s32Ret = SAMPLE_COMM_SYS_GetPicSize(gs_enNorm, enSize, &stSize); if (HI_SUCCESS != s32Ret) { SAMPLE_PRT("SAMPLE_COMM_SYS_GetPicSize failed!\n"); goto END_VENC_USER_2; } s32Ret = SAMPLE_COMM_VENC_Start(VencGrp, VencChn, PT_H264, gs_enNorm, enSize, SAMPLE_RC_CBR); if (HI_SUCCESS != s32Ret) { SAMPLE_PRT("Start snap failed!\n"); goto END_VENC_USER_2; } /****************************************** step 6: stream venc process -- get stream, then save it to file. ******************************************/ s32Ret = SAMPLE_COMM_VENC_StartGetStream(u32ChnCnt); if (HI_SUCCESS != s32Ret) { SAMPLE_PRT("StartGetStream failed!\n"); goto END_VENC_USER_3; } u32PicLStride = CEILING_2_POWER(stSize.u32Width, SAMPLE_SYS_ALIGN_WIDTH); u32PicCStride = CEILING_2_POWER(stSize.u32Width, SAMPLE_SYS_ALIGN_WIDTH); u32LumaSize = (u32PicLStride * stSize.u32Height); u32ChrmSize = (u32PicCStride * stSize.u32Height) >> 2; while(0 == feof(pfp_img)) { SAMPLE_MEMBUF_S stMem = {0}; VIDEO_FRAME_INFO_S stFrmInfo; stMem.hPool = hPool; u32Cnt ++; while((stMem.hBlock = HI_MPI_VB_GetBlock(stMem.hPool, u32BlkSize,NULL)) == VB_INVALID_HANDLE) { ; } stMem.u32PhyAddr = HI_MPI_VB_Handle2PhysAddr(stMem.hBlock); stMem.pVirAddr = (HI_U8 *) HI_MPI_SYS_Mmap( stMem.u32PhyAddr, u32BlkSize ); if(stMem.pVirAddr == NULL) { SAMPLE_PRT("Mem dev may not open\n"); goto END_VENC_USER_4; } memset(&stFrmInfo.stVFrame, 0, sizeof(VIDEO_FRAME_S)); stFrmInfo.stVFrame.u32PhyAddr[0] = stMem.u32PhyAddr; stFrmInfo.stVFrame.u32PhyAddr[1] = stFrmInfo.stVFrame.u32PhyAddr[0] + u32LumaSize; stFrmInfo.stVFrame.u32PhyAddr[2] = stFrmInfo.stVFrame.u32PhyAddr[1] + u32ChrmSize; stFrmInfo.stVFrame.pVirAddr[0] = stMem.pVirAddr; stFrmInfo.stVFrame.pVirAddr[1] = (HI_U8 *) stFrmInfo.stVFrame.pVirAddr[0] + u32LumaSize; stFrmInfo.stVFrame.pVirAddr[2] = (HI_U8 *) stFrmInfo.stVFrame.pVirAddr[1] + u32ChrmSize; stFrmInfo.stVFrame.u32Width = stSize.u32Width; stFrmInfo.stVFrame.u32Height = stSize.u32Height; stFrmInfo.stVFrame.u32Stride[0] = u32PicLStride; stFrmInfo.stVFrame.u32Stride[1] = u32PicLStride; stFrmInfo.stVFrame.u32Stride[2] = u32PicLStride; stFrmInfo.stVFrame.u64pts = (u32Cnt * 40); stFrmInfo.stVFrame.u32TimeRef = (u32Cnt * 2); /* Different channsel with different picture sequence */ SAMPLE_COMM_VENC_ReadOneFrame( pfp_img, stFrmInfo.stVFrame.pVirAddr[0], stFrmInfo.stVFrame.pVirAddr[1], stFrmInfo.stVFrame.pVirAddr[2], stFrmInfo.stVFrame.u32Width, stFrmInfo.stVFrame.u32Height, stFrmInfo.stVFrame.u32Stride[0], stFrmInfo.stVFrame.u32Stride[1] >> 1 ); SAMPLE_COMM_VENC_PlanToSemi( stFrmInfo.stVFrame.pVirAddr[0], stFrmInfo.stVFrame.u32Stride[0], stFrmInfo.stVFrame.pVirAddr[1], stFrmInfo.stVFrame.u32Stride[1], stFrmInfo.stVFrame.pVirAddr[2], stFrmInfo.stVFrame.u32Stride[1], stFrmInfo.stVFrame.u32Width, stFrmInfo.stVFrame.u32Height ); stFrmInfo.stVFrame.enPixelFormat = SAMPLE_PIXEL_FORMAT; stFrmInfo.stVFrame.u32Field = VIDEO_FIELD_FRAME; stMem.u32PoolId = HI_MPI_VB_Handle2PoolId( stMem.hBlock ); stFrmInfo.u32PoolId = stMem.u32PoolId; s32Ret = HI_MPI_VENC_SendFrame(VencGrp, &stFrmInfo); HI_MPI_SYS_Munmap( stMem.pVirAddr, u32BlkSize ); HI_MPI_VB_ReleaseBlock(stMem.hBlock); } /****************************************** step 7: exit process ******************************************/ END_VENC_USER_4: SAMPLE_COMM_VENC_StopGetStream(); END_VENC_USER_3: s32Ret = SAMPLE_COMM_VENC_Stop(VencGrp,VencChn);; if (HI_SUCCESS != s32Ret) { SAMPLE_PRT("Stop encode failed!\n"); goto END_VENC_USER_2; } END_VENC_USER_2: //before destroy private pool,must stop venc HI_MPI_VB_DestroyPool( hPool ); END_VENC_USER_1: //close the yuv file fclose( pfp_img ); pfp_img = HI_NULL; END_VENC_USER_0: //system exit SAMPLE_COMM_SYS_Exit(); return s32Ret; }
/****************************************************************************** * function : Get from YUV ******************************************************************************/ HI_S32 SAMPLE_COMM_VI_GetVFrameFromYUV(FILE *pYUVFile, HI_U32 u32Width, HI_U32 u32Height,HI_U32 u32Stride, VIDEO_FRAME_INFO_S *pstVFrameInfo) { HI_U32 u32LStride; HI_U32 u32CStride; HI_U32 u32LumaSize; HI_U32 u32ChrmSize; HI_U32 u32Size; VB_BLK VbBlk; HI_U32 u32PhyAddr; HI_U8 *pVirAddr; u32LStride = u32Stride; u32CStride = u32Stride; u32LumaSize = (u32LStride * u32Height); u32ChrmSize = (u32CStride * u32Height) >> 2;/* YUV 420 */ u32Size = u32LumaSize + (u32ChrmSize << 1); /* alloc video buffer block ---------------------------------------------------------- */ VbBlk = HI_MPI_VB_GetBlock(VB_INVALID_POOLID, u32Size, NULL); if (VB_INVALID_HANDLE == VbBlk) { SAMPLE_PRT("HI_MPI_VB_GetBlock err! size:%d\n",u32Size); return -1; } u32PhyAddr = HI_MPI_VB_Handle2PhysAddr(VbBlk); if (0 == u32PhyAddr) { return -1; } pVirAddr = (HI_U8 *) HI_MPI_SYS_Mmap(u32PhyAddr, u32Size); if (NULL == pVirAddr) { return -1; } pstVFrameInfo->u32PoolId = HI_MPI_VB_Handle2PoolId(VbBlk); if (VB_INVALID_POOLID == pstVFrameInfo->u32PoolId) { return -1; } SAMPLE_PRT("pool id :%d, phyAddr:%x,virAddr:%x\n" ,pstVFrameInfo->u32PoolId,u32PhyAddr,(int)pVirAddr); pstVFrameInfo->stVFrame.u32PhyAddr[0] = u32PhyAddr; pstVFrameInfo->stVFrame.u32PhyAddr[1] = pstVFrameInfo->stVFrame.u32PhyAddr[0] + u32LumaSize; pstVFrameInfo->stVFrame.u32PhyAddr[2] = pstVFrameInfo->stVFrame.u32PhyAddr[1] + u32ChrmSize; pstVFrameInfo->stVFrame.pVirAddr[0] = pVirAddr; pstVFrameInfo->stVFrame.pVirAddr[1] = pstVFrameInfo->stVFrame.pVirAddr[0] + u32LumaSize; pstVFrameInfo->stVFrame.pVirAddr[2] = pstVFrameInfo->stVFrame.pVirAddr[1] + u32ChrmSize; pstVFrameInfo->stVFrame.u32Width = u32Width; pstVFrameInfo->stVFrame.u32Height = u32Height; pstVFrameInfo->stVFrame.u32Stride[0] = u32LStride; pstVFrameInfo->stVFrame.u32Stride[1] = u32CStride; pstVFrameInfo->stVFrame.u32Stride[2] = u32CStride; pstVFrameInfo->stVFrame.enPixelFormat = PIXEL_FORMAT_YUV_SEMIPLANAR_420; pstVFrameInfo->stVFrame.u32Field = VIDEO_FIELD_INTERLACED;/* Intelaced D1,otherwise VIDEO_FIELD_FRAME */ /* read Y U V data from file to the addr ----------------------------------------------*/ SAMPLE_COMM_VI_ReadFrame(pYUVFile, pstVFrameInfo->stVFrame.pVirAddr[0], pstVFrameInfo->stVFrame.pVirAddr[1], pstVFrameInfo->stVFrame.pVirAddr[2], pstVFrameInfo->stVFrame.u32Width, pstVFrameInfo->stVFrame.u32Height, pstVFrameInfo->stVFrame.u32Stride[0], pstVFrameInfo->stVFrame.u32Stride[1] >> 1 ); /* convert planar YUV420 to sem-planar YUV420 -----------------------------------------*/ SAMPLE_COMM_VI_PlanToSemi(pstVFrameInfo->stVFrame.pVirAddr[0], pstVFrameInfo->stVFrame.u32Stride[0], pstVFrameInfo->stVFrame.pVirAddr[1], pstVFrameInfo->stVFrame.u32Stride[1], pstVFrameInfo->stVFrame.pVirAddr[2], pstVFrameInfo->stVFrame.u32Stride[1], pstVFrameInfo->stVFrame.u32Width, pstVFrameInfo->stVFrame.u32Height); HI_MPI_SYS_Munmap(pVirAddr, u32Size); return 0; }