Page 1 of 1

Segmentation Faul Error

Posted: 2025-04-16 8:17
by quocan123
Hi, I have try to modify luckfox_pici_rtsp_opencv example. I'm try to add VPSS to source code, but when it's fail, error is Segmentation Faul( Core Dump).
Please help me check which part is wrong. Thank you!!
this is my main code:

Code: Select all

int main(int argc, char *argv[]) {
  system("RkLunch-stop.sh");
	RK_S32 s32Ret = 0; 

	int width    = DISP_WIDTH;
    int height   = DISP_HEIGHT;

	char fps_text[16];
	float fps = 0;
	memset(fps_text,0,16);

	//h264_frame	
	VENC_STREAM_S stFrame;	
	stFrame.pstPack = (VENC_PACK_S *)malloc(sizeof(VENC_PACK_S));
	RK_U64 H264_PTS = 0;
	RK_U32 H264_TimeRef = 0; 
	VIDEO_FRAME_INFO_S stViFrame;
	
	// Create Pool
	MB_POOL_CONFIG_S PoolCfg;
	memset(&PoolCfg, 0, sizeof(MB_POOL_CONFIG_S));
	PoolCfg.u64MBSize = width * height * 3 ;
	printf("%d\n",atoi(argv[1]));
	RK_U32 blk = (RK_U32)atoi(argv[1]);
	PoolCfg.u32MBCnt = blk;
	printf("%d\n",blk);
	PoolCfg.enAllocType = MB_ALLOC_TYPE_DMA;
	//PoolCfg.bPreAlloc = RK_FALSE;
	MB_POOL src_Pool = RK_MPI_MB_CreatePool(&PoolCfg);
	printf("Create Pool success !\n");	

	// Get MB from Pool 
	MB_BLK src_Blk = RK_MPI_MB_GetMB(src_Pool, width * height * 3, RK_TRUE);
	
	// Build h264_frame
	VIDEO_FRAME_INFO_S h264_frame;
	h264_frame.stVFrame.u32Width = width;
	h264_frame.stVFrame.u32Height = height;
	h264_frame.stVFrame.u32VirWidth = width;
	h264_frame.stVFrame.u32VirHeight = height;
	h264_frame.stVFrame.enPixelFormat =  RK_FMT_RGB888; 
	h264_frame.stVFrame.u32FrameFlag = 160;
	h264_frame.stVFrame.pMbBlk = src_Blk;
	unsigned char *data = (unsigned char *)RK_MPI_MB_Handle2VirAddr(src_Blk);
	cv::Mat frame(cv::Size(width,height),CV_8UC3,data);
	
	// rkaiq init
	RK_BOOL multi_sensor = RK_FALSE;	
	const char *iq_dir = "/etc/iqfiles";
	rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
	//hdr_mode = RK_AIQ_WORKING_MODE_ISP_HDR2;
	SAMPLE_COMM_ISP_Init(0, hdr_mode, multi_sensor, iq_dir);
	SAMPLE_COMM_ISP_Run(0);

	// rkmpi init
	if (RK_MPI_SYS_Init() != RK_SUCCESS) {
		RK_LOGE("rk mpi sys init fail!");
		return -1;
	}

	// rtsp init	
	rtsp_demo_handle g_rtsplive = NULL;
	rtsp_session_handle g_rtsp_session;
	g_rtsplive = create_rtsp_demo(554);
	g_rtsp_session = rtsp_new_session(g_rtsplive, "/live/0");
	rtsp_set_video(g_rtsp_session, RTSP_CODEC_ID_VIDEO_H264, NULL, 0);
	rtsp_sync_video_ts(g_rtsp_session, rtsp_get_reltime(), rtsp_get_ntptime());

	// vi init
	vi_dev_init();
	vi_chn_init(0, width, height);
	if(vpss_init(0,width, height)== RK_SUCCESS){
		printf("VPSS INIT SUCCESS\n");
	}
	// venc init
	RK_CODEC_ID_E enCodecType = RK_VIDEO_ID_AVC;
	venc_init(0, width, height, enCodecType);
	MPP_CHN_S stSrcChn,stvpssChn;
	stSrcChn.enModId = RK_ID_VI;
	stSrcChn.s32ChnId =0;
	stSrcChn.s32DevId = 0;

	stvpssChn.enModId = RK_ID_VPSS;
	stvpssChn.s32ChnId =0;
	stvpssChn.s32DevId = 0;

	RK_MPI_SYS_Bind(&stSrcChn,&stvpssChn);
	printf("init success\n");	
	
	while(1) {			
		// get vi frame
		h264_frame.stVFrame.u32TimeRef = H264_TimeRef++;
		h264_frame.stVFrame.u64PTS = TEST_COMM_GetNowUs(); 
		// s32Ret = RK_MPI_VI_GetChnFrame(0, 0, &stViFrame, -1);
		s32Ret = RK_MPI_VPSS_GetChnFrame(0,0,&stViFrame, -1);
		if(s32Ret == RK_SUCCESS)
		{
			void *vi_data = RK_MPI_MB_Handle2VirAddr(stViFrame.stVFrame.pMbBlk);

			cv::Mat yuv420sp(height + height / 2, width, CV_8UC1, vi_data);
			cv::Mat bgr(height, width, CV_8UC3, data);	
			// cv::rotate(bgr,bgr,ROTATION_180);		
			cv::cvtColor(yuv420sp, bgr, cv::COLOR_YUV420sp2BGR);
			cv::resize(bgr, frame, cv::Size(width ,height), 0, 0, cv::INTER_LINEAR);
			sprintf(fps_text,"fps = %.2f",fps);		
            cv::putText(frame,fps_text,
							cv::Point(40, 40),
							cv::FONT_HERSHEY_SIMPLEX,1,
							cv::Scalar(0,255,0),2);
			
		}
		memcpy(data, frame.data, width * height * 3);
		
		// encode H264	
		RK_MPI_VENC_SendFrame(0,  &h264_frame ,-1);
	
		// rtsp
		s32Ret = RK_MPI_VENC_GetStream(0, &stFrame, -1);	
		if(s32Ret == RK_SUCCESS) {
			if(g_rtsplive && g_rtsp_session) {
				//printf("len = %d PTS = %d \n",stFrame.pstPack->u32Len, stFrame.pstPack->u64PTS);	
				void *pData = RK_MPI_MB_Handle2VirAddr(stFrame.pstPack->pMbBlk);
				rtsp_tx_video(g_rtsp_session, (uint8_t *)pData, stFrame.pstPack->u32Len,
							  stFrame.pstPack->u64PTS);
				rtsp_do_event(g_rtsplive);
			}
			RK_U64 nowUs = TEST_COMM_GetNowUs();
			fps = (float) 1000000 / (float)(nowUs - h264_frame.stVFrame.u64PTS);			
		}

		// release frame 
		s32Ret = RK_MPI_VI_ReleaseChnFrame(0, 0, &stViFrame);
		if (s32Ret != RK_SUCCESS) {
			RK_LOGE("RK_MPI_VI_ReleaseChnFrame fail %x", s32Ret);
		}
		s32Ret = RK_MPI_VENC_ReleaseStream(0, &stFrame);
		if (s32Ret != RK_SUCCESS) {
			RK_LOGE("RK_MPI_VENC_ReleaseStream fail %x", s32Ret);
		}
	
	}

	// Destory MB
	RK_MPI_MB_ReleaseMB(src_Blk);
	// Destory Pool
	RK_MPI_MB_DestroyPool(src_Pool);

	RK_MPI_VI_DisableChn(0, 0);
	RK_MPI_VI_DisableDev(0);

	RK_MPI_VPSS_StopGrp(0);
	RK_MPI_VPSS_DestroyGrp(0);
		
	SAMPLE_COMM_ISP_Stop(0);

	RK_MPI_VENC_StopRecvFrame(0);
	RK_MPI_VENC_DestroyChn(0);

	free(stFrame.pstPack);

	if (g_rtsplive)
		rtsp_del_demo(g_rtsplive);
	
	RK_MPI_SYS_Exit();

	return 0;
}

And this is error:

Code: Select all

=== 0 ===
RTDeviceV4L2     15:57:56-766 {ispStreamOn       :511} do ispStreamOn done
vpss_init
rga_api version 1.10.1_[0]
VPSS INIT SUCCESS
venc_init
MpiAdapterEncode 15:57:56-780 {ma_encode_create  :1743} ringbuf buf_size 3112960
MpiAdapterEncode 15:57:56-788 {ma_encode_create  :1782} mem_fd = 55
init success
=== 1 ===
MpiAdapterEncode 15:57:56-863 {ma_encode_get_pack:2079} map ptr: 0xa3d9e000, fd 56
[DEBUG utils.c:134:rtsp_codec_data_parse_from_user_h264] sps 30
[DEBUG utils.c:142:rtsp_codec_data_parse_from_user_h264] pps 4
Segmentation fault (core dumped)

Re: Segmentation Faul Error

Posted: 2025-04-16 8:35
by Crocodile
Hello, the latest version of RKMPI does not support taking frames directly from VPSS, that is, RK_MPI_VPSS_GetChnFrame cannot be used, kernel 5.10.110 in the old version of the VPSS component is supported.
We do not participate in maintenance RKMPI and there is no related source code, there is a need to take frames from VPSS can only use the old version of SDK

Re: Segmentation Faul Error

Posted: 2025-04-16 9:03
by quocan123
I see on the wiki that Luck Fox Pico can take frames up to 23fps. But when I run an example, it is just about 11fps. How can I improve FPS?

Re: Segmentation Faul Error

Posted: 2025-04-16 10:56
by Crocodile
The main cause of frame rate loss is the use of OpenCV-Mobile for image format conversion, which is intended to make the code more flexible for secondary development. To improve the frame rate, there are two approaches:

1 Use RKRGA to accelerate the image format conversion process.
2 Avoid using OpenCV-Mobile and instead use OSD (On-Screen Display) methods to annotate the image. Although this approach is more complex than using OpenCV-Mobile, it is much more efficient and is also the method recommended by Rockchip. Relevant examples can be found in ${SDK}/media.

The wiki uses an older version of the SDK and implements image format conversion through VPSS. Like the RKRGA library, VPSS also utilizes the 2D acceleration engine of the RV1106 for color format conversion.

Re: Segmentation Faul Error

Posted: 2025-04-17 1:41
by quocan123
Can you give document about how to install and how to use RKRGA library please

Re: Segmentation Faul Error

Posted: 2025-04-17 1:59
by Crocodile
The document refers to the attachment, using a simple routine that I have written https://github.com/luckfox-eng29/luckfox_pico_rga

Re: Segmentation Faul Error

Posted: 2025-04-17 2:25
by quocan123
Thank you so much!!