SIGSEGV when accessing NvBufSurface with OpenCV

DeepStreamSDK 5.1.0

  • NVIDIA Jetson AGX Xavier [16GB]
    • Jetpack 4.5.1 [L4T 32.5.2]
    • NV Power Mode: MAXN - Type: 0
    • jetson_stats.service: active
  • Libraries:
    • CUDA: 10.2.89
    • cuDNN: 8.0.0.180
    • TensorRT: 7.1.3.0
    • Visionworks: 1.6.0.501
    • OpenCV: 4.4.0 compiled CUDA: YES
    • VPI: ii libnvvpi1 1.0.15 arm64 NVIDIA Vision Programming Interface library
    • Vulkan: 1.2.70

Hi all,

I am compiling a custom plugin which will perform some operations over a 16bit video file.

image

I want to use OpenCV functions with CUDA support and therefore the first step is to get the NvBufSurface into a cv::cuda::GpuMat.

Using the plugin gst-dsexample as reference we have the following code:

static GstFlowReturn
get_converted_mat(NvBufSurface *input_buf, gint idx, gint height, gint width)
{
	NvBufSurface ip_surf;
	ip_surf = *input_buf;
	std::cout << "Inside get_converted_mat" << std::endl;
	ip_surf.surfaceList = &(input_buf->surfaceList[idx]);
	std::cout << "ip_surf.surfaceList: " << typeid(ip_surf.surfaceList[0]).name() << std::endl;
	std::cout << "width x height: " << width << " x " << height << std::endl;

	cv::Mat identity = cv::Mat::eye(3, 3, CV_32FC1);
    // Upload it to the GPU
    cv::cuda::GpuMat identity_gpu(identity);
    // Use the OpenCV cuda method for summation (OK)
    cv::Scalar res = cv::cuda::sum(identity_gpu);
    std::cout << "res=" << res << std::endl;

	cv::cuda::GpuMat gpuMat = cv::cuda::GpuMat(
		height, 
		width, 
		CV_8UC3, 
		ip_surf.surfaceList[0].dataPtr
	);
}
// Most important function, which gets called for every input buffer (equivalent of chain function but allows inplace transformations, faster too)
static GstFlowReturn gst_transform_ip(GstBaseTransform* base, GstBuffer* buf)
{
	GstVisualizeThermalPlugin* visualize_thermal_plugin = GST_VISUALIZE_THERMAL_PLUGIN(base);
	
	CHECK_CUDA_STATUS(cudaSetDevice(0), "Unable to set cuda device");

	GstMapInfo in_map_info;
	memset(&in_map_info, 0, sizeof(GstMapInfo)); // TODO: this might be unnecessary
	if (!gst_buffer_map(buf, &in_map_info, GST_MAP_READ))
	{
		g_print("Error: Failed to map gst buffer\n");
		gst_buffer_unmap(buf, &in_map_info);
		return GST_FLOW_ERROR;
	}

	gint height = visualize_thermal_plugin->processing_height;
	gint width = visualize_thermal_plugin->processing_width;

	NvBufSurface* surface = (NvBufSurface *)in_map_info.data;
	get_converted_mat(surface, 0, 512, 640);

	GST_INFO_OBJECT(visualize_thermal_plugin, "Transform_IP");
	gst_buffer_unmap(buf, &in_map_info);
	return GST_FLOW_OK;
}

The output I am getting is the following:

$ gst-launch-1.0 filesrc location=/home/power/POWER_Data/SDCard/DataSink/11_03_2022_18_57_17/TnFoV_record_11_03_2022_18_57_17.avi ! visualize_thermal_clahe ! multifilesink location='test_out/test-frame%07d.png'
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Inside get_converted_mat
ip_surf.surfaceList: 18NvBufSurfaceParams
width x height: 640 x 512
res=[3, 0, 0, 0]
Caught SIGSEGV
#0  0x0000007fa64b8ef8 in __GI___poll (fds=0x558a23ff30, nfds=548251919240, timeout=<optimized out>) at ../sysdeps/unix/sysv/linux/poll.c:41
#1  0x0000007fa65c5f58 in  () at /usr/lib/aarch64-linux-gnu/libglib-2.0.so.0
#2  0x0000005589cb9330 in  ()
Spinning.  Please run 'gdb gst-launch-1.0 16716' to continue debugging, Ctrl-C to quit, or Ctrl-\ to dump core.

I am only getting started with gstreamer and deepstream so I would appreciate any help I can get.

Thanks in advance!

Hi @kevin.serrano , Could you add some log or use the gdb tool to lacate which function cause the crash? we cannot do future anlysis just from your crash info.