海思IVE硬件加速算子使用

如若算子涉及使用的图像类型为海思私有格式,可直接内存拷贝后使用。

如若算子涉及使用的图像为文件流操作读入uchar中则需经过以下操作进行写入海思私有格式中(代码参考如下)

注:一下代码均为大致思路,如若需要完整调用接口等请评论处留下邮箱。

        1、MPI_Init

//mpi初始化
        s32Ret = HI_MPI_VB_SetConf(&struVbConf);
	if (HI_SUCCESS != s32Ret)
	{
		SAMPLE_PRT("HI_MPI_VB_SetConf fail,Error(%#x)\n", s32Ret);
		return s32Ret;
	}
	s32Ret = HI_MPI_VB_Init();
	if (HI_SUCCESS != s32Ret)
	{
		SAMPLE_PRT("HI_MPI_VB_Init fail,Error(%#x)\n", s32Ret);
		return s32Ret;
	}
	struSysConf.u32AlignWidth = 64;
	s32Ret = HI_MPI_SYS_SetConf(&struSysConf);
	if (HI_SUCCESS != s32Ret)
	{
		SAMPLE_PRT("HI_MPI_SYS_SetConf fail,Error(%#x)\n", s32Ret);
		(HI_VOID)HI_MPI_VB_Exit();
		return s32Ret;
	}

	s32Ret = HI_MPI_SYS_Init();
	if (HI_SUCCESS != s32Ret)
	{
		SAMPLE_PRT("HI_MPI_SYS_Init fail,Error(%#x)\n", s32Ret);
		(HI_VOID)HI_MPI_VB_Exit();
		return s32Ret;
	}

        2、初始化IVE中涉及使用的变量

//初始化变量
        s32Ret = HISI_IVE_CreateImageByCached(&(pstSobel->stSrc1), IVE_IMAGE_TYPE_U8C1, u16Width, u16Height);
	if (s32Ret != HI_SUCCESS)
	{
		SAMPLE_PRT("HISI_IVE_CreateImageByCached fail\n");
		goto SOBEL_INIT_FAIL;
	}
	s32Ret = HISI_IVE_CreateImageByCached(&(pstSobel->stSrc2), IVE_IMAGE_TYPE_U8C1, u16Width, u16Height);
	if (s32Ret != HI_SUCCESS)
	{
		SAMPLE_PRT("HISI_IVE_CreateImageByCached fail\n");
		goto SOBEL_INIT_FAIL;
	}
    
        HI_S32 HISI_IVE_CreateImageByCached(IVE_IMAGE_S* pstImg,IVE_IMAGE_TYPE_E enType, HI_U16 u16Width, HI_U16 u16Height)
{
	HI_U32 u32Size = 0;
	HI_S32 s32Ret;
	if (NULL == pstImg)
	{
		SAMPLE_PRT("pstImg is null\n");
		return HI_FAILURE;
	}

	pstImg->enType = enType;
	pstImg->u16Width = u16Width;
	pstImg->u16Height = u16Height;
	pstImg->u16Stride[0] = HISI_IVE_CalcStride(pstImg->u16Width, IVE_ALIGN);

	switch (enType)
	{
	    case IVE_IMAGE_TYPE_U8C1:
	    case IVE_IMAGE_TYPE_S8C1:
	    {
		    u32Size = pstImg->u16Stride[0] * pstImg->u16Height;
		    s32Ret = HI_MPI_SYS_MmzAlloc_Cached(&pstImg->u32PhyAddr[0], (void**)&pstImg->pu8VirAddr[0], NULL, HI_NULL, u32Size);
		    if (s32Ret != HI_SUCCESS)
		    {
		        SAMPLE_PRT("Mmz Alloc fail,Error(%#x)\n", s32Ret);
			return s32Ret;
		    }
	    }
		

	}

	return HI_SUCCESS;

}

        3、将uchar读入至私有格式中

//读入私有格式
        switch (pstImg->enType)
	{
		case  IVE_IMAGE_TYPE_U8C1:
		{
			pU8 = pstImg->pu8VirAddr[0];
			//for (y = 0; y < height; y++)
			{
				memcpy(pU8, frame_src, width * height);

				//pU8 += pstImg->u16Stride[0];
			}
		}
                	break;
		case  IVE_IMAGE_TYPE_YUV420SP:
		{
			pU8 = pstImg->pu8VirAddr[0];
			for (y = 0; y < height; y++)
			{
				memcpy(pU8, frame_src, width);

				pU8 += pstImg->u16Stride[0];
			}

			pU8 = pstImg->pu8VirAddr[1];
			for (y = 0; y < height / 2; y++)
			{
				memcpy(pU8, frame_src, width);

				pU8 += pstImg->u16Stride[1];
			}
		}
        }

        4、IVE算子的调用

              算子调用请见海思标准IVE api文档

你可能感兴趣的:(图像,海思IVE)