Hi!
I am just installed DS4.0 on my xavier.
& trying to use new Optical Flow functionality.
Here’s pipeline:
gst-launch-1.0 filesrc location=/opt/nvidia/deepstream/deepstream-4.0/samples/streams/sample_720p.h264 ! h264parse ! nvv4l2decoder ! m.sink_0 nvstreammux name=m batch-size=1 width=1280 height=720 ! nvvideoconvert ! nvof ! dsexample full-frame=1 ! fakesink
How to get “OF” meta in dsexample plug-in?
using gst_buffer_get_nvds_batch_meta?
function gst_buffer_get_nvds_meta exists in header but not in libs :(
meta type: NVDS_OPTICAL_FLOW_META
NvDsOpticalFlowMeta in nvds_opticalflow_meta.h
static GstFlowReturn
gst_nvof_visual_transform_internal(GstBaseTransform *btrans,
GstBuffer *inbuf, GstBuffer *outbuf)
{
GstNvOFVisual *ofvisual = GST_NV_OF_VISUAL (btrans);
GstFlowReturn flow_ret = GST_FLOW_OK;
gpointer state = NULL;
gboolean of_metadata_found = FALSE;
GstMeta *gst_meta = NULL;
NvDsMeta *dsmeta = NULL;
NvDsBatchMeta *batch_meta = NULL;
guint i = 0;
GstMapInfo outmap;
START_PROFILE;
char context_name[100];
snprintf(context_name, sizeof(context_name), "%s_(Frame=%" G_GUINT64_FORMAT ")",
GST_ELEMENT_NAME(ofvisual), ofvisual->frame_num);
nvtx_helper_push_pop(context_name);
if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE))
{
g_print ("%s output buf map failed\n", __func__);
return GST_FLOW_ERROR;
}
NvBufSurface *dstSurf = (NvBufSurface *)outmap.data;
gst_buffer_unmap (outbuf, &outmap);
// Required in the case of tiler
if (!gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_META, 0, -1)) {
GST_DEBUG ("Buffer metadata copy failed \n");
}
GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (inbuf);
if (cudaSetDevice(ofvisual->gpu_id) != cudaSuccess)
{
g_printerr("Error: failed to set GPU to %d\n", ofvisual->gpu_id);
return GST_FLOW_ERROR;
}
while ((gst_meta = gst_buffer_iterate_meta (inbuf, &state)))
{
if (gst_meta_api_type_has_tag(gst_meta->info->api, _dsmeta_quark))
{
dsmeta = (NvDsMeta *) gst_meta;
if (dsmeta->meta_type == NVDS_BATCH_GST_META) {
batch_meta = (NvDsBatchMeta *)dsmeta->meta_data;
break;
}
}
}
if (batch_meta == NULL)
{
g_print ("batch_meta not found, skipping optical flow visual draw execution\n");
return GST_FLOW_ERROR;
}
dstSurf->numFilled = batch_meta->num_frames_in_batch;
// TODO: Improve by mapping and unmapping buffer at the time of buffer creation
NvBufSurfaceMap (dstSurf, -1, -1, NVBUF_MAP_WRITE);
#if 1
NvBufSurfaceSyncForCpu (dstSurf, -1, -1);
for (i=0; i < batch_meta->num_frames_in_batch; i++)
{
NvDsFrameMeta *frame_meta = nvds_get_nth_frame_meta (batch_meta->frame_meta_list, i);
if (frame_meta->frame_user_meta_list)
{
NvDsFrameMetaList *fmeta_list = NULL;
NvDsUserMeta *of_user_meta = NULL;
for (fmeta_list = frame_meta->frame_user_meta_list; fmeta_list != NULL; fmeta_list = fmeta_list->next)
{
of_user_meta = (NvDsUserMeta *)fmeta_list->data;
if (of_user_meta && of_user_meta->base_meta.meta_type == NVDS_OPTICAL_FLOW_META)
{
NvDsOpticalFlowMeta *ofmeta = (NvDsOpticalFlowMeta *) (of_user_meta->user_meta_data);
void * dst = NULL;
if (ofmeta)
{
#if defined(__aarch64__)
CUresult status;
CUeglFrame eglFrame;
CUgraphicsResource pResource = NULL;
EGLImageKHR eglimage_dst = NULL;
if (dstSurf->surfaceList[i].mappedAddr.eglImage == NULL)
{
NvBufSurfaceMapEglImage (dstSurf, -1);
}
eglimage_dst = dstSurf->surfaceList[i].mappedAddr.eglImage;
status = cuGraphicsEGLRegisterImage(&pResource, eglimage_dst,
CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
if (status != CUDA_SUCCESS)
{
printf("cuGraphicsEGLRegisterImage failed: %d\n", status);
exit (-1);
}
status = cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
if (status != CUDA_SUCCESS)
{
printf("cuGraphicsSubResourceGetMappedArray failed\n");
}
dst = (void*) eglFrame.frame.pPitch[0];
#else
dst = (void*) dstSurf->surfaceList[i].mappedAddr.addr[0];
#endif
#if 1
// Cuda Kernel based Drawing
DrawOpticalFlow_Cuda (ofmeta->data, dst, ofmeta->cols, ofmeta->rows, 10.0, ofvisual->streams_array[i]);
#else
// CPU based Drawing
DrawOpticalFlow (ofmeta->data, dst, ofmeta->cols, ofmeta->rows, 10.0);
#endif
of_metadata_found = TRUE;
#if defined(__aarch64__)
status = cuGraphicsUnregisterResource(pResource);
if (status != CUDA_SUCCESS) {
printf ("cuGraphicsEGLUnRegisterResource failed: %d \n", status);
}
#endif
}
}
}
}
}
for (i=0; i < batch_meta->num_frames_in_batch; i++)
{
cudaStreamSynchronize (ofvisual->streams_array[i]);
}
NvBufSurfaceSyncForDevice (dstSurf, -1, -1);
#else
static int add = 0xf;
NvBufSurfaceMemSet (dstSurf, -1, -1, add);
add += 1;
#endif
NvBufSurfaceUnMap (dstSurf, -1, -1);
if (of_metadata_found == FALSE)
{
GST_WARNING_OBJECT (ofvisual, "OF METADATA NOT FOUND\n");
}
nvtx_helper_push_pop(NULL);
STOP_PROFILE("+++++++++ NVOFVISUAL BUFFER PROCESSED +++++++++");
return flow_ret;
}