I am able to access the surface but the image written is of incorrect resolution.
I followed that same thread. it worked for single source but still couldn’t get the correct output for multiple sources. Here is my code:
void write_frames(GstBuffer *buf){
GstMapInfo in_map_info;
NvBufSurface *surface = NULL;
memset (&in_map_info, 0, sizeof (in_map_info));
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;
}
cudaError_t cuda_err;
NvBufSurfTransformRect src_rect, dst_rect;
NvBufSurfTransformRect src_rect1, dst_rect1;
surface = (NvBufSurface *) in_map_info.data;
g_print("\nBathc_Size: %d\n",surface->batchSize);
int batch_size= surface->batchSize;
cudaStream_t cuda_stream;
NvBufSurfaceCreateParams nvbufsurface_create_params, nvbufsurface_create_params1;
NvBufSurfTransformParams nvbufsurface_params;
NvBufSurfTransformParams nvbufsurface_params1;
NvBufSurface *dst_surface = NULL;
NvBufSurface *dst_surface1 = NULL;
src_rect.top = 0;
src_rect.left = 0;
src_rect.width = (guint) surface->surfaceList[0].width;
src_rect.height = (guint) surface->surfaceList[0].height;
dst_rect.top = 0;
dst_rect.left = 0;
dst_rect.width = (guint) surface->surfaceList[0].width;
dst_rect.height= (guint) surface->surfaceList[0].height;
nvbufsurface_params.src_rect = &src_rect;
nvbufsurface_params.dst_rect = &dst_rect;
nvbufsurface_params.transform_flag = NVBUFSURF_TRANSFORM_CROP_SRC | NVBUFSURF_TRANSFORM_CROP_DST;
nvbufsurface_params.transform_filter = NvBufSurfTransformInter_Default;
nvbufsurface_create_params.gpuId = surface->gpuId;
nvbufsurface_create_params.width = (gint) surface->surfaceList[0].width;
nvbufsurface_create_params.height = (gint) surface->surfaceList[0].height;
nvbufsurface_create_params.size = 0;
nvbufsurface_create_params.colorFormat = NVBUF_COLOR_FORMAT_RGBA;
nvbufsurface_create_params.layout = NVBUF_LAYOUT_PITCH;
nvbufsurface_create_params.memType = NVBUF_MEM_DEFAULT;
src_rect1.top = 0;
src_rect1.left = 0;
src_rect1.width = (guint) surface->surfaceList[1].width;
src_rect1.height = (guint) surface->surfaceList[1].height;
dst_rect1.top = 0;
dst_rect1.left = 0;
dst_rect1.width = (guint) surface->surfaceList[1].width;
dst_rect1.height= (guint) surface->surfaceList[1].height;
nvbufsurface_params1.src_rect = &src_rect1;
nvbufsurface_params1.dst_rect = &dst_rect1;
nvbufsurface_params1.transform_flag = NVBUFSURF_TRANSFORM_CROP_SRC | NVBUFSURF_TRANSFORM_CROP_DST;
nvbufsurface_params1.transform_filter = NvBufSurfTransformInter_Default;
nvbufsurface_create_params1.gpuId = surface->gpuId;
nvbufsurface_create_params1.width = (gint) surface->surfaceList[1].width;
nvbufsurface_create_params1.height = (gint) surface->surfaceList[1].height;
nvbufsurface_create_params1.size = 0;
nvbufsurface_create_params1.colorFormat = NVBUF_COLOR_FORMAT_RGBA;
nvbufsurface_create_params1.layout = NVBUF_LAYOUT_PITCH;
nvbufsurface_create_params1.memType = NVBUF_MEM_DEFAULT;
cuda_err = cudaSetDevice (surface->gpuId);
cuda_err=cudaStreamCreate (&cuda_stream);
int create_result = NvBufSurfaceCreate(&dst_surface,batch_size,&nvbufsurface_create_params);
create_result = NvBufSurfaceCreate(&dst_surface1,batch_size,&nvbufsurface_create_params1);
NvBufSurfTransformConfigParams transform_config_params;
NvBufSurfTransform_Error err;
transform_config_params.compute_mode = NvBufSurfTransformCompute_Default;
transform_config_params.gpu_id = surface->gpuId;
transform_config_params.cuda_stream = cuda_stream;
err = NvBufSurfTransformSetSessionParams (&transform_config_params);
NvBufSurfaceMemSet (dst_surface, 0, 0, 0);
err = NvBufSurfTransform (surface, dst_surface, &nvbufsurface_params);
if (err != NvBufSurfTransformError_Success) {
g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err);
}
NvBufSurfaceMemSet (dst_surface1, 1, 0, 0);
err = NvBufSurfTransform (surface, dst_surface1, &nvbufsurface_params1);
if (err != NvBufSurfTransformError_Success) {
g_print ("NvBufSurfTransform1 failed with error %d while converting buffer\n", err);
}
char filename[94];
NvBufSurfaceMap (dst_surface, 0, 0, NVBUF_MAP_READ);
NvBufSurfaceSyncForCpu (dst_surface, 0, 0);
NvBufSurfaceMap (dst_surface1, 1, 0, NVBUF_MAP_READ);
NvBufSurfaceSyncForCpu (dst_surface1, 1, 0);
cv::Mat bgr_frame = cv::Mat (cv::Size(nvbufsurface_create_params.width, nvbufsurface_create_params.height), CV_8UC3);
cv::Mat in_mat = cv::Mat (nvbufsurface_create_params.height, nvbufsurface_create_params.width,
CV_8UC4, dst_surface->surfaceList[0].mappedAddr.addr[0],
dst_surface->surfaceList[0].pitch);
cv::cvtColor (in_mat, bgr_frame, CV_RGBA2BGR);
snprintf(filename, 94, "ds_media/%07d_Camera_0.jpg", frame_count[0]++);
cv::imwrite(filename,bgr_frame);
cv::Mat bgr_frame1 = cv::Mat (cv::Size(nvbufsurface_create_params1.width, nvbufsurface_create_params1.height), CV_8UC3);
cv::Mat in_mat1 = cv::Mat (nvbufsurface_create_params1.height, nvbufsurface_create_params1.width,
CV_8UC4, dst_surface1->surfaceList[1].mappedAddr.addr[0],
dst_surface1->surfaceList[1].pitch);
cv::cvtColor (in_mat1, bgr_frame1, CV_RGBA2BGR);
snprintf(filename, 94, "ds_media/%07d_Camera_1.jpg", frame_count[1]++);
cv::imwrite(filename,bgr_frame1);
NvBufSurfaceUnMap (dst_surface, 0, 0);
NvBufSurfaceDestroy (dst_surface);
NvBufSurfaceUnMap (dst_surface1, 1, 0);
NvBufSurfaceDestroy (dst_surface1);
cudaStreamDestroy (cuda_stream);
gst_buffer_unmap (buf, &in_map_info);
return;
}
The code prints batch-size correctly - 2. I also tried saving only the second surface but to no avail.