Hi,
Provide the patch as below.
diff --git a/gst-nvv4l2camera/Makefile b/gst-nvv4l2camera/Makefile
index 3dab045..4815208 100644
--- a/gst-nvv4l2camera/Makefile
+++ b/gst-nvv4l2camera/Makefile
@@ -1,4 +1,5 @@
-# Copyright (c) 2019-2022, NVIDIA CORPORATION. All rights reserved.
+# SPDX-FileCopyrightText: Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@@ -30,15 +31,37 @@
TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)
+ifdef CUDA_VER
+CUDA_PATH := /usr/local/cuda-$(CUDA_VER)
+else
+CUDA_PATH := /usr/local/cuda
+endif
+
+# Location of the target rootfs
+ifeq ($(shell uname -m), aarch64)
+TARGET_ROOTFS :=
+else
+ifeq ($(TARGET_ROOTFS),)
+$(error Please specify the target rootfs path if you are cross-compiling)
+endif
+endif
+
+ALGO_CUDA_DIR=cuda
+
GST_INSTALL_DIR?=/usr/lib/aarch64-linux-gnu/gstreamer-1.0/
LIB_INSTALL_DIR?=/usr/lib/aarch64-linux-gnu/tegra/
CFLAGS:=
-LIBS:= -lnvbufsurface -lnvbufsurftransform
+LIBS:= -lnvbufsurface -lnvbufsurftransform \
+ -lcuda -lcudart \
+ -L"$(TARGET_ROOTFS)/$(CUDA_PATH)/lib64" \
SRCS := $(wildcard *.cpp)
-INCLUDES += -I./ -I../
+INCLUDES += -I./ -I../ -I./cuda \
+ -I/usr/src/jetson_multimedia_api/include/ \
+ -I$(CUDA_PATH)/include
+
PKGS := gstreamer-1.0 \
gstreamer-base-1.0 \
@@ -48,6 +71,8 @@
libv4l2
OBJS := $(SRCS:.cpp=.o)
+OBJS += $(ALGO_CUDA_DIR)/NvCudaProc.o
+
CFLAGS += -fPIC \
-DEXPLICITLY_ADDED=1 \
@@ -66,6 +91,9 @@
%.o: %.cpp
$(CC) -c $< $(CFLAGS) $(INCLUDES) -o $@
+$(ALGO_CUDA_DIR)/%.o: $(ALGO_CUDA_DIR)/%.cpp
+ $(CC) -c $< $(CFLAGS) $(INCLUDES) -o $@
+
$(SO_NAME): $(OBJS)
$(CC) -shared -o $(SO_NAME) $(OBJS) $(LIBS) $(LDFLAGS)
diff --git a/gst-nvv4l2camera/cuda/NvCudaProc.cpp b/gst-nvv4l2camera/cuda/NvCudaProc.cpp
new file mode 100644
index 0000000..1c4d198
--- /dev/null
+++ b/gst-nvv4l2camera/cuda/NvCudaProc.cpp
@@ -0,0 +1,173 @@
+/*
+ * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of NVIDIA CORPORATION nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <cuda_runtime_api.h>
+#include "cudaEGL.h"
+#include "NvCudaProc.h"
+
+static void
+Handle_EGLImageMapping(EGLImageKHR srcimage,
+ EGLImageKHR dstimage,
+ int width,
+ int height,
+ int bytesPerPix);
+
+static void
+doMapping(CUdeviceptr srcDevPtr,
+ CUdeviceptr dstDevPtr,
+ int pitch,
+ int width,
+ int height,
+ int bytesPerPix);
+
+void
+HandleEGLImageMapping(void *pSrcEGLImage,
+ void *pDstEGLImage,
+ int width,
+ int height,
+ int bytesPerPix)
+{
+ EGLImageKHR *pSrcImage = (EGLImageKHR *)pSrcEGLImage;
+ EGLImageKHR *pDstImage = (EGLImageKHR *)pDstEGLImage;
+ Handle_EGLImageMapping(pSrcImage, pDstImage, width, height, bytesPerPix);
+}
+
+static void
+Handle_EGLImageMapping(EGLImageKHR srcimage,
+ EGLImageKHR dstimage,
+ int width,
+ int height,
+ int bytesPerPix)
+{
+ CUresult status;
+
+ CUeglFrame srcEglFrame;
+ CUgraphicsResource pSrcResource = NULL;
+ CUeglFrame dstEglFrame;
+ CUgraphicsResource pDstResource = NULL;
+
+ cudaFree(0);
+ status = cuGraphicsEGLRegisterImage(&pSrcResource, srcimage,
+ CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsEGLRegisterImage failed: %d, cuda process stop\n",
+ status);
+ return;
+ }
+
+ status = cuGraphicsEGLRegisterImage(&pDstResource, dstimage,
+ CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsEGLRegisterImage failed: %d, cuda process stop\n",
+ status);
+ return;
+ }
+
+ status = cuGraphicsResourceGetMappedEglFrame(&srcEglFrame, pSrcResource, 0, 0);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsSubResourceGetMappedArray failed\n");
+ }
+
+ status = cuGraphicsResourceGetMappedEglFrame(&dstEglFrame, pDstResource, 0, 0);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsSubResourceGetMappedArray failed\n");
+ }
+
+ status = cuCtxSynchronize();
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuCtxSynchronize failed\n");
+ }
+
+ if (srcEglFrame.frameType == CU_EGL_FRAME_TYPE_PITCH)
+ {
+ doMapping((CUdeviceptr) srcEglFrame.frame.pPitch[0],
+ (CUdeviceptr) dstEglFrame.frame.pPitch[0],
+ dstEglFrame.pitch,
+ width,
+ height,
+ bytesPerPix);
+ }
+
+ status = cuCtxSynchronize();
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuCtxSynchronize failed after memcpy\n");
+ }
+
+ status = cuGraphicsUnregisterResource(pSrcResource);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsEGLUnRegisterResource failed: %d\n", status);
+ }
+
+ status = cuGraphicsUnregisterResource(pDstResource);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuGraphicsEGLUnRegisterResource failed: %d\n", status);
+ }
+}
+
+
+static void
+doMapping(CUdeviceptr srcDevPtr,
+ CUdeviceptr dstDevPtr,
+ int pitch,
+ int width,
+ int height,
+ int bytesPerPix)
+{
+ CUDA_MEMCPY2D copyParams = {0};
+ copyParams.srcMemoryType = CU_MEMORYTYPE_DEVICE;
+ copyParams.srcDevice = srcDevPtr;
+ copyParams.srcPitch = width * bytesPerPix;
+ copyParams.srcXInBytes = 0;
+ copyParams.srcY = 0;
+
+ copyParams.dstMemoryType = CU_MEMORYTYPE_DEVICE;
+ copyParams.dstDevice = dstDevPtr;
+ copyParams.dstPitch = pitch;
+ copyParams.dstXInBytes = 0;
+ copyParams.dstY = 0;
+
+ copyParams.WidthInBytes = width * bytesPerPix;
+ copyParams.Height = height;
+
+ // Perform the copy
+ CUresult status = cuMemcpy2D(©Params);
+ if (status != CUDA_SUCCESS)
+ {
+ printf("cuMemcpy2D failed: %d\n", status);
+ }
+}
diff --git a/gst-nvv4l2camera/cuda/NvCudaProc.h b/gst-nvv4l2camera/cuda/NvCudaProc.h
new file mode 100644
index 0000000..e65fcb9
--- /dev/null
+++ b/gst-nvv4l2camera/cuda/NvCudaProc.h
@@ -0,0 +1,48 @@
+/*
+* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+* SPDX-License-Identifier: BSD-3-Clause
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+* * Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* * Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in the
+* documentation and/or other materials provided with the distribution.
+* * Neither the name of NVIDIA CORPORATION nor the names of its
+* contributors may be used to endorse or promote products derived
+* from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef __NVCUDAPROC_H
+#define __NVCUDAPROC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* This function is called when padding insertion is needed for proper alignment.
+ * The function registers surfaces to EGL images for CUDA access and uses cuMemcpy2D
+ * to perform correct alignment.
+ * */
+void HandleEGLImageMapping(void *pSrcEGLImage, void *pDstEGLImage, int width, int height,
+ int bytesPerPix);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/gst-nvv4l2camera/gstnvv4l2camerasrc.cpp b/gst-nvv4l2camera/gstnvv4l2camerasrc.cpp
index b211f66..1a04f64 100644
--- a/gst-nvv4l2camera/gstnvv4l2camerasrc.cpp
+++ b/gst-nvv4l2camera/gstnvv4l2camerasrc.cpp
@@ -1,5 +1,7 @@
/*
- * Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
+
+ * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -55,6 +57,7 @@
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
+#include "NvCudaProc.h"
/* Capture caps of plugin */
#define CAPTURE_CAPS \
@@ -198,6 +201,19 @@
goto error;
}
+ if (nvbuf->surface->surfaceList[0].pitch !=
+ nvbuf->surface->surfaceList[0].planeParams.bytesPerPix[0] *
+ nvbuf->surface->surfaceList[0].width && !self->remap_surf) {
+ ret = NvBufSurfaceAllocate (&self->remap_surf, 1, ¶m);
+
+ if (ret != 0) {
+ GST_ERROR ("%s: NvBufSurfaceAllocate Failed \n", __func__);
+ goto error;
+ }
+
+ self->remap_surf->numFilled = 1;
+ }
+
nvbuf->surface->numFilled = 1;
gst_memory_init (GST_MEMORY_CAST (mem), flags, allocator, NULL,
@@ -242,6 +258,23 @@
GST_DEBUG ("%s:\n", __func__);
+ GstNVV4l2MemoryAllocator *nvmm_allocator = GST_NVMEMORY_ALLOCATOR (allocator);
+ if (!nvmm_allocator) {
+ GST_DEBUG ("%s: NULL GstNVV4l2MemoryAllocator \n", __func__);
+ return;
+ }
+
+ GstNvV4l2CameraSrc *self = (GstNvV4l2CameraSrc*) nvmm_allocator->owner;
+ if (self == NULL) {
+ GST_DEBUG ("%s: NULL NvV4l2CameraSrc \n", __func__);
+ return;
+ }
+
+ if (self->remap_surf) {
+ NvBufSurfaceDestroy(self->remap_surf);
+ self->remap_surf = NULL;
+ }
+
GstNVV4l2Memory *nv_mem = (GstNVV4l2Memory *) mem;
if (nv_mem == NULL)
return;
@@ -376,6 +409,28 @@
memcpy (nv_mem->nvcam_buf->buffer, &tmp_v4l2_buf, sizeof (struct v4l2_buffer));
}
+ if(src->remap_surf) {
+ if (-1 == NvBufSurfaceMapEglImage(nv_mem->nvcam_buf->surface, 0)){
+ GST_ERROR_OBJECT(pool, "NvBufSurfaceMapEglImage failed");
+ }
+
+ if (-1 == NvBufSurfaceMapEglImage(src->remap_surf, 0)){
+ GST_ERROR_OBJECT(pool, "NvBufSurfaceMapEglImage failed");
+ }
+
+ HandleEGLImageMapping(nv_mem->nvcam_buf->surface->surfaceList[0].mappedAddr.eglImage,
+ src->remap_surf->surfaceList[0].mappedAddr.eglImage,
+ nv_mem->nvcam_buf->surface->surfaceList[0].width,
+ nv_mem->nvcam_buf->surface->surfaceList[0].height,
+ nv_mem->nvcam_buf->surface->surfaceList[0].planeParams.bytesPerPix[0]);
+
+ /* Destroy EGLImage */
+ NvBufSurfaceUnMapEglImage(nv_mem->nvcam_buf->surface, 0);
+ NvBufSurfaceUnMapEglImage(src->remap_surf, 0);
+
+ NvBufSurfaceCopy (src->remap_surf, nv_mem->nvcam_buf->surface);
+ }
+
return ret;
no_memblk:
diff --git a/gst-nvv4l2camera/gstnvv4l2camerasrc.hpp b/gst-nvv4l2camera/gstnvv4l2camerasrc.hpp
index e126dee..3eb80c7 100644
--- a/gst-nvv4l2camera/gstnvv4l2camerasrc.hpp
+++ b/gst-nvv4l2camera/gstnvv4l2camerasrc.hpp
@@ -1,5 +1,6 @@
/*
- * Copyright (c) 2019-2022, NVIDIA CORPORATION. All rights reserved.
+ * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -78,6 +79,7 @@
struct v4l2_capability *caps;
struct v4l2_format *fmt;
struct v4l2_requestbuffers *req;
+ NvBufSurface *remap_surf;
gint index;
guint cap_buf;
Thanks