海思抓图功能的实现

    海思3518ev200芯片的sdk中没有专用的抓图接口,在特定场合下需要jpeg图片的情况下,采用将h264数据编码为图片比较麻烦,因此可以采用海思自己的JPEG编码。

    不多说直接上代码:

char *pic_addr;
int VencFd, s32Ret;
INT32U maxfd = 0;

struct timeval TimeoutVal;
fd_set read_fds;
VENC_CHN_STAT_S stStat;
VENC_STREAM_S stStream;	
VIDEO_FRAME_INFO_S stFrame;

if (HI_MPI_VI_SetFrameDepth(0, 1))    //设置VI通道缓存的视频图像帧数   必须的
{
    printf("HI_MPI_VI_SetFrameDepth err, vi chn %d \n", 0);
     return -1;
}

usleep(90 * 1000);

VencFd = HI_MPI_VENC_GetFd(1);        //JPEG编码通道为1,获取通道句柄
if (VencFd < 0)
{
    printf("HI_MPI_VENC_GetFd failed with %#x!\n", VencFd);
    return -1;
}
	
maxfd = VencFd;
	
s32Ret = HI_MPI_VI_GetFrame(0, &stFrame, VIDEO_GET_FRAME_TIMEOUT);        //从VI获取一张原画
if (HI_SUCCESS != s32Ret)
{
    printf("HI_MPI_VI_GetFrame ext chn(%d) fail,Error(%#x)\n", 0 ,s32Ret);
    return -1;
}
	
s32Ret = HI_MPI_VENC_SendFrame(1, &stFrame, VIDEO_GET_FRAME_TIMEOUT);       //将VI原画发送到JPEG编码器
if (HI_SUCCESS != s32Ret)
{
    printf("HI_MPI_VENC_SendFrame ext chn(%d) fail,Error(%#x)\n", 1 ,s32Ret);
    return -1;
}

while(1)
{
    FD_ZERO(&read_fds);
    FD_SET(VencFd, &read_fds);
		
    TimeoutVal.tv_sec  = 0;
    TimeoutVal.tv_usec = 50 * 1000;
    s32Ret = select(maxfd + 1, &read_fds, NULL, NULL, &TimeoutVal);        //等待JPEG编码完成
    if (s32Ret < 0)
    {
        printf("select failed!\n");
        return -1;
    }
    else if (s32Ret == 0)
    {
        printf("get venc stream time out\n");
        continue;
    }
    else
    {
        if (FD_ISSET(VencFd, &read_fds))
        {
            memset(&stStream, 0, sizeof(stStream));
            s32Ret = HI_MPI_VENC_Query(1, &stStat);        //查询编码通道状态,是否创建成功
	    if (VIDEO_SUCC != s32Ret)
	    {
	        printf("HI_MPI_VENC_Query chn[%d] failed with %#x!\n", 1, s32Ret);
		return -1;
	    }
				
	    if(0 == stStat.u32CurPacks)
            {
		printf("NOTE: Current frame is NULL!\n");
		return -1;
	    }
	    stStream.pstPack = (VENC_PACK_S*)malloc(sizeof(VENC_PACK_S) * stStat.u32CurPacks);
	    if (NULL == stStream.pstPack)
	    {
		printf("malloc stream pack failed!\n");
		return -1;
	    }
			
	    stStream.u32PackCount = stStat.u32CurPacks;
	    s32Ret = HI_MPI_VENC_GetStream(1, &stStream, VIDEO_GET_FRAME_TIMEOUT);        //获取JPEG图片
	    if (VIDEO_SUCC != s32Ret)
            {
                free(stStream.pstPack);
                stStream.pstPack = NULL;
                printf("HI_MPI_VENC_GetStream failed with %#x!\n", s32Ret);
                return -1;
            }
	    else
	    {
	    	printf("\npicture data len is %d\n", stStream.pstPack->u32Len);
		pic_addr = (char *)stStream.pstPack[0].pu8Addr + stStream.pstPack[0].u32Offset;
					
		memcpy(picture_data, pic_addr, stStream.pstPack->u32Len);
		*data_len = stStream.pstPack->u32Len;
		break;
	    }
        }
    }	
free(stStream.pstPack);
HI_MPI_VI_ReleaseFrame(0, &stFrame);        //释放

你可能感兴趣的:(海思抓图功能的实现)